How to have different child classes of a template base class in a container?

V

Vikas

I have a template class called Base with child classes called Child1
and Child2 as follows:

template <typename T>
class Base
{

};

class Child1 : Base<Concrete1>
{

};

class Child2 : Base<Concrete2>
{

};

Concrete1 and Concrete2 classes are derived from a class called
ConcreteBase.

Now I need to create a list containing both Child1 and Child2 but the
following doesn't work.

std::list<Base<ConcreteBase> *> BaseList;

How can I create the above list?

Thanks for your replies.

Vikas
 
V

Victor Bazarov

Vikas said:
I have a template class called Base with child classes called Child1
and Child2 as follows:

template <typename T>
class Base
{
.
};

class Child1 : Base<Concrete1>
{
.
};

class Child2 : Base<Concrete2>
{
.
};

Concrete1 and Concrete2 classes are derived from a class called
ConcreteBase.

Now I need to create a list containing both Child1 and Child2 but the
following doesn't work.

std::list<Base<ConcreteBase> *> BaseList;

How can I create the above list?

You cannot. Your only option is

class BaseOfBases {
...
};

template<class T>
class Base : public BaseOfBases {
...
};

std::list<BaseOfBases*> BaseList;

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top