I have heard that C++ class member function templates can\'t be virtual. Is this true?
If they can be virtual, what is an example of a scenario in which one would
The following code can be compiled and runs properly, using MinGW G++ 3.4.5 on Window 7:
#include
#include
using namespace std;
template
class A{
public:
virtual void func1(const T& p)
{
cout<<"A:"<
class B
: public A
{
public:
virtual void func1(const T& p)
{
cout<<"A<--B:"< a;
B b;
B c;
A* p = &a;
p->func1("A a");
p = dynamic_cast*>(&c);
p->func1("B c");
B* q = &b;
q->func1(3);
}
and the output is:
A:A a
A<--B:B c
A<--B:3
And later I added a new class X:
class X
{
public:
template
virtual void func2(const T& p)
{
cout<<"C:"<
When I tried to use class X in main() like this:
X x;
x.func2("X x");
g++ report the following error:
vtempl.cpp:34: error: invalid use of `virtual' in template declaration of `virtu
al void X::func2(const T&)'
So it is obvious that: