Inheritance, Constructors, Virtual Methods and typeid
Posted by Kaya Kupferschmidt • Wednesday, May 3. 2006 • Category: C++
When working with inheritance and virtual methods, there might be some surprises when one tries to call a virtual method inside a constructor - and this is the reasons why one should never ever call virtual methods inside a constructor or inside a destructor.
Consider the following simple code
Consider the following simple code
class A {
public:
A() {
printf("A::A() calls ");
f();
};
~A() {
printf("A::~A() calls ");
f();
};
virtual void f() {
printf("A::f()\n");
};
};
class B : public A {
public:
B() {
printf("B::B() calls ");
f();
};
~B() {
printf("B::~B() calls ");
f();
};
virtual void f() {
printf("B::f()\n");
};
};
void main(void) {
B obj;
obj.f();
};
Continue reading "Inheritance, Constructors, Virtual Methods and typeid"


