You can easily verify this:
#include <stdio.h>
class A{
public:
A(){ puts("A()"); }
~A(){ puts("~A()"); }
};
class B: public A{
public:
B(){ puts("B()"); }
~B(){ puts("~B()"); }
};
class C: public B{
public:
C(){ puts("C()"); }
~C(){ puts("~C()"); }
};
class D: public C{
public:
D(){ puts("D()"); }
~D(){ puts("~D()"); }
};
int main(){
/* puts("\nConstructing A:");
A myA;
puts("\nConstructing B:");
B myB;
puts("\nConstructing C:");
C myC;
*/
puts("\nConstructing D:");
D myD;
return 0;
}
(I commented out A-C definitions, so the destructor output does not get mixed up. Feel free to uncomment them.)
Main Topics
Browse All Topics





by: stefan73Posted on 2004-12-13 at 13:48:35ID: 12813692
Sure. In the class hierarchy, the root object is constructed first, then the inheritance chain is traversed "downward".
In the destruction, the order is opposite.