Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to have different child classes of a template base class in a container?

Reply
Thread Tools

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

 
 
Vikas
Guest
Posts: n/a
 
      08-25-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2003
"Vikas" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Pointers to base class (invoking methods of child classes) Alfonso Morra C++ 4 09-01-2005 02:06 PM
Re: template class derived from non-template base class Matt Graham C++ 0 07-21-2003 09:02 PM



Advertisments