In a simplistic design, a class B inherits a class A polymorphically.
A templated class, Base<T> has a T* member that is used for further operations. A Derived<T> inherits from Base<T> polymorphically.
What would be the syntax that allows this kind of object creation:
Base<A>* a = new Derived<B>();
For further reference, the code I used looks like this (of course, the conversion does not succeed):
class A
{
public:
A()
{
cout<< " A ";
}
virtual void one()
{
cout<<" 1 ";
}
};
class B: public A
{
public:
B()
{
cout << " B ";
}
void one()
{
cout << " 2 ";
}
};
template <class T>
class Base
{
public:
T* thing;
Base()
{
cout<<"Base";
thing = new T;
}
template <class S>
Base(Base<S>* obj)
{
thing = obj->thing;
}
virtual void poly(){ thing->one();}
};
template <class T>
class Derived : public Base<T>
{
public:
Derived()
{
cout << "DERIVED ";
}
virtual void poly(){
};
int main(int argc, char** argv)
{
//Base<A>* a = (new Derived<B>());
return 0;
}
Virtual destructors and proper memory management omitted on purpose for code brevity.
EDIT : the sole purpose of this construction would be to keep, say, a list of BaseTemplated<BasePolymorphic>* pointers together, and not use BaseTemplated<Derived1> to BaseTemplated<DerivedN> for all N subclasses of a base, polymorphic class.
First, tell me when we get to the “simplistic” part.
Peel back all half of the templates to just concentrate on the part you’re trying to polymorph). Eventually you have distinct classes
Base<A>andBase<B>. (the latter through derivation ofDerived<B>).Neither of these inherit from their respective template parameters. Therefore the relationship (hierarchical or otherwise) of
AtoBis irrelevant.Base<A>andBase<B>are distinct and unrelated, and therefore what you’re trying to do as-writen cannot work. In fact, even if the did inherit from aAandBrespectively, the very best you could hope for is anA*pointer, which you aren’t using in your sample.And I’ll gladly delete this if shown otherwise, because I’m genuinely curious.