Re: Problem with template
lhommedumatch wrote:
I have a derived template class and I want to call a method of the
derived class from the base class.
But the method need a template parameter.
How can I do?
Make the base class a template as well, and make that function a pure
virtual function which every derived class must override.
Here is the code that doesn't work.
#include <iostream>
#include <string>
using namespace std;
//==============================================================
Add here:
template<class T>
class A
{
public:
A()
{
std::cout << "constructeur de A" << std::endl;
}
Add here
virtual
~A()
{
std::cout << "destructeur de A" << std::endl;
}
Add here:
virtual void setValue(T) = 0;
};
//==============================================================
template <class T> class B: public A
Add here
<T>
{
public:
B():A()
{
std::cout << "constructeur de B" << std::endl;
}
~B()
{
std::cout << "destructeur de B" << std::endl;
}
T _value;
void setValue(T p_value)
{
_value = p_value;
}
};
//==============================================================
int main()
{
A *a = new B<int>;
a->setValue(23131);
delete a;
If the destructor of A isn't virtual, the deletion of 'a' pointer will
have undefined behaviour.
}
//==============================================================
Thanks
YW
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask