By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Vikas" <vs*****@hotmail.com> wrote...
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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.