some more inheritance with template

Discussion in 'C++' started by Tony Johansson, Aug 16, 2005.

  1. Hello Experts!

    I reading a book called programming with design pattern revealed
    by Tomasz Muldner and here I read something that I don't understand
    completely.

    Im I right if I say the following.
    In this case is the derived class a class template and the base class a
    class template.
    Both the base class and the derived class will be instansiated with some
    specific type T
    template <typename T>
    class MyStack : public Stack<T> {. . .}

    You cannot derive a concreate class from a class template as in
    class IntStack : public Stack<T,100> {. . .} why.
    Is it because the base class will not be instantiated with a type.
    I mean if the base class could be instansiated with a type then this kind of
    inheritance should be valid.

    What does this text try to say..
    "Instantiations of template are unrelated, even if classes for these
    instantiations
    are in a inheritance relation(that is, one is derived from the other). For
    example,
    given a general BankAccount class that has a derived class CheckingAccount,
    there is no inheritance relation between Stack<BankAccount> and
    Stack<CheckingAccount>".

    Many thanks

    //Tony
    Tony Johansson, Aug 16, 2005
    #1
    1. Advertising

  2. Tony Johansson

    benben Guest

    I think what the text is trying to say is that you cannot derive IntStack
    from Stack without instantiating Stack template with a specific type.

    Put it this way:
    a class template is NOT a type;
    an instantiation of a class template, however, is a type (e.g.
    Stack<int, 100>);
    you can only inherit from a type.

    So if T is not a type declared/defined previously, then Stack<T> is not a
    type, you can't derive from it.

    For example:

    class PatientRecord{};

    template <typename T>
    class Stack{};

    template <typename T>
    class SafeStack: public Stack<T> // OK
    {
    // Ok, Stack<T> is a type where
    // T is a template parameter type.
    };

    class PatientRecordStack: public Stack<PatientRecord>
    {
    // Ok, Stack<PatientRecord> is a type
    };

    class SomeStack: public Stack<T>
    {
    // Error, Stack<T> is not a type,
    // because T is not defined.
    };
    benben, Aug 16, 2005
    #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. Chris Theis
    Replies:
    2
    Views:
    465
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    532
    tom_usenet
    Jul 24, 2003
  3. Michael
    Replies:
    4
    Views:
    408
    Matt Hammond
    Jun 26, 2006
  4. Tony Johansson
    Replies:
    7
    Views:
    346
    Victor Bazarov
    Aug 16, 2005
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    215
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page