some more inheritance with template

T

Tony Johansson

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
 
B

benben

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.
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top