I know there are few threads about this topic. But what really confused me is the result I got is different from what everyone is saying.
Look this code below (compiled using GCC441):
#include <iostream>
using namespace std;
template<class T>
class A {
public:
A(T &t) : _a(t) {};
virtual ~A() { cout << "Dtor-A" << endl;};
virtual void print () { cout << "A: " << _a << endl; }
T _a;
};
class B : public A<int> {
public:
B(int t) : A<int>(t) {}
~B() { cout << "Dtor-B" << endl;};
void print() { cout << "B: " << endl; }
};
int main() {
B b(2);
A<int> *a = &b;
a->print();
A<int> *a2 = new B(4);
a2->print();
delete a2;
}
The result is:
B:
B:
Dtor-B
Dtor-A
Dtor-B
Dtor-A
If virtual function is not allowed in template class, why did I get this result?