template inheritance

Discussion in 'C++' started by zl2k, Aug 17, 2006.

  1. zl2k

    zl2k Guest

    hi, all
    Suppose I have a template class A

    template<class T> class A{...}

    Then I have another class B which inherits class A. Should I say

    class B: public A{...}

    or class B: template<class T> A{...}

    or template<class T> class B : public A {...}

    or something else? I assume that class A is also a template. But can
    class A be a non template class? If yes, how can A handle the different
    type of T? Thanks ahead.

    zl2k
    zl2k, Aug 17, 2006
    #1
    1. Advertising

  2. template<class T>
    class Derived : public Base<T> {

    };

    --

    Frederick Gotham
    Frederick Gotham, Aug 17, 2006
    #2
    1. Advertising

  3. zl2k schrieb:
    > hi, all
    > Suppose I have a template class A
    >
    > template<class T> class A{...}
    >
    > Then I have another class B which inherits class A. Should I say
    >
    > class B: public A{...}
    >
    > or class B: template<class T> A{...}
    >
    > or template<class T> class B : public A {...}


    Depends.

    If you want class B to be a template like class A, then:

    template <class T>
    class B : public A<T>
    {
    // ...
    };

    If you want that class B inherits from a specific class A, say with
    template parameter int:

    class B : public A<int>
    {
    // ...
    };

    --
    Thomas
    Thomas J. Gritzan, Aug 17, 2006
    #3
  4. zl2k

    Guest

    zl2k wrote:
    > hi, all
    > Suppose I have a template class A
    >
    > template<class T> class A{...}


    No, that's a class template. A is not a class. A<int> and A<char> are
    classes.

    > Then I have another class B which inherits class A. Should I say


    Same problem, you can have a class B<int> which inherits from A<int>
    but
    remember that A is not a class.

    > class B: public A{...}
    >
    > or class B: template<class T> A{...}
    >
    > or template<class T> class B : public A {...}
    >
    > or something else? I assume that class A is also a template. But can
    > class A be a non template class? If yes, how can A handle the different
    > type of T? Thanks ahead.


    template B<T> can inherit from any class. When you instantiate it, you
    have
    a type for parameter T. Thus, you can also instantiate template A<T> as
    a
    base class, or A<std::pair<T,T> >, or std::eek:stream. The base class
    doesn't
    have to be a template instantiation. In fact, it often isn't. The base
    class has
    no more need to "handle" T as any member of B has. Only your design
    will
    tell you whether the base class needs to know about T, and how. (E.g.
    it
    might be a non-template class with a templated contructor)

    Regards,
    Michiel Salters
    , Aug 17, 2006
    #4
  5. zl2k

    zl2k Guest

    wrote:
    > zl2k wrote:
    > > hi, all
    > > Suppose I have a template class A
    > >
    > > template<class T> class A{...}

    >
    > No, that's a class template. A is not a class. A<int> and A<char> are
    > classes.
    >
    > > Then I have another class B which inherits class A. Should I say

    >
    > Same problem, you can have a class B<int> which inherits from A<int>
    > but
    > remember that A is not a class.
    >
    > > class B: public A{...}
    > >
    > > or class B: template<class T> A{...}
    > >
    > > or template<class T> class B : public A {...}
    > >
    > > or something else? I assume that class A is also a template. But can
    > > class A be a non template class? If yes, how can A handle the different
    > > type of T? Thanks ahead.

    >
    > template B<T> can inherit from any class. When you instantiate it, you
    > have
    > a type for parameter T. Thus, you can also instantiate template A<T> as
    > a
    > base class, or A<std::pair<T,T> >, or std::eek:stream. The base class
    > doesn't
    > have to be a template instantiation. In fact, it often isn't. The base
    > class has
    > no more need to "handle" T as any member of B has. Only your design
    > will
    > tell you whether the base class needs to know about T, and how. (E.g.
    > it
    > might be a non-template class with a templated contructor)
    >
    > Regards,
    > Michiel Salters


    Thanks for all the comments, very helpful. Have a good day.
    zl2k
    zl2k, Aug 17, 2006
    #5
    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. Chris Theis
    Replies:
    2
    Views:
    446
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    515
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,073
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    751
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    510
Loading...

Share This Page