C++ inheritance for classes that depend on one another [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to design an abstract class in C++. It is called AbstractCurve and it has
AbstractPoint's on it that are made up of x and y coordinates which are AbstractFieldElements.
I want to inherit from this to make, for example, a PrimeCurve that has PrimePoints and
PrimeFieldElements.
If I do something like this:
class AbstractPoint {
private:
AbstractFieldElement* x_;
AbstractFieldElement* y_;
};
How will I tell the compiler to make x_ and y_ be PrimeFieldElements when I create
the PrimePoint class?
Also, is there some better way to do this that I am missing?

Assuming the only difference between the classes is these datatypes, I would do something like this:
template <typename ElementType>
class Point {
private:
ElementType* x_;
ElementType* y_;
};
using AbstractPoint = Point<AbstractFieldElement>;
using PrimePoint = Point<PrimeFieldElement>;
If we take this to the next level up, we can keep going
template <typename ElementType>
class Curve {
private:
using Point = Point<ElementType>;
std::vector<Point> points_;
};
using AbstractCurve = Curve<AbstractFieldElement>;
using PrimeFieldCure = Curve<PrimeFieldElement>;
It is reasonable to use inheritance as you are suggesting to provide common behavior to a variety of curve/point types, but if the different types will use different internal data types, then templates are the way to go.

Related

Using generic template member in a class C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.

C++ non-type template template parameters, reference to *this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to improve my generic architecture, and I've decided that a system that uses this type of ambiguous context referencing would be ideal for my purposes. However, I'm having trouble figuring out how to get the syntax to work. I don't even know if something like this is possible! I have slightly shiestier alternatives that accomplish mostly the same thing but this would be best:
class IContained
{
public:
virtual int getInt() = 0;
};
typedef std::shared_ptr<IContained>IContainedPtr;
template<template<class RefType, RefType& itsRef> class ContainedType>
class TestClass
{
TestClass() :
myContained(new ContainedType < TestClass, *this>())
{
}
int getContextInt()
{
return 3;
}
IContainedPtr myContained;
};
template<class RefType, RefType& itsRef>
class Contained:
virtual public IContained
{
int getInt()
{
return itsRef.getContextInt();
}
};
TEST(POTATO, PARTY)
{
TestClass<Contained> myTest();
int thing = myTest.myContained->getInt();
EXPECT_EQ(thing, 3);
}
I am not sure what do you want to implement but I can explain why you are not able to use
*this
as template argument. Templates provide you with compile-time polymorphism. It means that all templates arguments should be known at compile time.
this
is a class instance variable, is an address of class instance, so it could not be determined during compilation. The same as
*this

questiong regarding memory optimzation C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have header file as something like following.
class A;
class B;
class C;
Class a {
A *a;
B *b;
C *c;
};
Now, Class a does not using all A, B, C and around 40 others. I have around 40 forward declarations... Is possible to optimize it someone.. So, I can declared pointer to class on need bases instead of wasting memory for all pointer to all 40 odd class?
You can use union with type code or boost::variant
I would recommend for now that you should make a parent class and put as children all of the A,B,C,etc... Then in class use a list of the parent class, and put whatever subclass you need on it. But having this problem is actually due to wrong object oriented design. Learn the principles of OO design of a system in Java for example and then put them in use in C++.
Admitting you cannot rework your classes, and admitting you are using only one at time, you can use a union plus an ID or a "dynamically typed void*":
unsigned gen_id()
{ static unsigned id=0; ++id; return id; }
template<class T>
unsigned id_of()
{ static id = gen_id(); return id; }
class a
{
void* m;
unsigned type;
public:
template<class T>
a(T* p) :m(p), type(id_of<T>())
{}
template<class T>
T* get() const
{ return (id_of<T>()==type)? static_cast<T*>(m): nullptr; }
};
You can access a data as
A* pa = my_a.get();
if(pa) { /* what has to be done with A */ }
If you need more than one, consider a class b holding a vector of a.
For a more "standardized" implementation you can look at boost::any

C++ Reference Length Performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My question is simple, what is the performance loss due to reference length. I cannot explain myself but here is the sample:
between this
C* pC = m_a->m_b->m_c;
and this expression
C* pC = m_b->m_c;
I am asking this because I have a global class which has a Singleton pattern and holds everything. I am accessing all of its members from its members like this.
class Global
{
A* a;
X* x;
};
class A { B* b; };
class B { C* c; }; // etc
class X { Y* y; };
class Y { Z* z; };
class Z
{
void foo() { Global::GetInstance()->a->b->c->foo(); }
}
Is this a good design? Any advice for this? I am having some trouble with this topic too Qt Architecture Advice Needed
Every -> operator is an indexed indirection, which costs a cycle or two, depending on the processor, and may be invisible if its pipeline is good enough.
However the real question here is 'compared to what?' What other implementation techniques are you considering for solving this problem? Unless you have a viable alternative your question is really meaningless.
Similarly the frequently-asked question about the relative efficiency of virtual and non-virtual functions is meaningless unless it takes into account how to get the same effect both ways. In the non-virtual case this amounts at least to an 'if' or 'switch', whose cost has to be added in to the comparison.

About delegates in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I specify the parameter of a method as any class that implements a specific interface ?
This is rather common in objective c.
There are no interfaces in standard C++, but we can simulate them pretty easily:
class IComparable
{
protected:
IComparable() {};
public:
virtual ~IComparable() = 0 {};
virtual int Compare(const IComparable& other) const = 0;
};
There is no way we can instantiate this class. It is effectively an interface. You can then derive concrete classes from this.
If you have an "interface" or abstract base class called Base, then a function which can accept any object implementing that interface would look like:
void fn(Base& obj) {
/*use Base functions on obj...*/
}