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

Discussion in 'C++' started by Vikas, Aug 25, 2003.

  1. Vikas

    Vikas Guest

    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
    Vikas, Aug 25, 2003
    #1
    1. Advertising

  2. "Vikas" <> 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
    Victor Bazarov, Aug 25, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    0
    Views:
    551
    Matt Graham
    Jul 21, 2003
  2. Tim Clacy
    Replies:
    12
    Views:
    605
    Dan W.
    Dec 3, 2003
  3. Alfonso Morra
    Replies:
    4
    Views:
    364
    Victor Bazarov
    Sep 1, 2005
  4. nguillot
    Replies:
    5
    Views:
    515
  5. Hicham Mouline
    Replies:
    1
    Views:
    580
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page