derivation of a template class

M

maadhuu

hello,
i want to know whether one can derive from a template class where the
derived class is also a template class....if this is true, then if i have
2 functions say f() in the parent class and g() in the derived class ,
where the function f() is called,then it gives an error ..why??
 
A

Alf P. Steinbach

* maadhuu:
i want to know whether one can derive from a template class where the
derived class is also a template class....

Yes; what were you thinking of might be in the way?

template< typename T > class A {};
if this is true, then if i have
2 functions say f() in the parent class and g() in the derived class ,
where the function f() is called,then it gives an error ..why??

Post the relevant code.
 
M

maadhuu

template<typename T>
class B {
public:
void f() { }
};

template<typename T>
class D : public B<T> {
public:
void g()
{
f(); // compiler gives an error here
}
};
this is the way it works......my question is whether this is allowed at
all i.e. deriving from a template class
 
A

Alf P. Steinbach

* maadhuu:
template<typename T>
class B {
public:
void f() { }
};

template<typename T>
class D : public B<T> {
public:
void g()
{
f(); // compiler gives an error here
}
};
this is the way it works......my question is whether this is allowed at
all i.e. deriving from a template class

Yes.

You just have to inform the compiler that f is an inherited member routine.

One way is to employ a 'using' directive in class D, another way is to
qualify the call with 'this->'.
 

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

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top