#include<iostream> using namespace std; class Base { public: Base() { cout << "Base:Constructor" <<endl; } ~Base() { cout << "Base:Destructor" << endl; } }; class DerivedA:public Base { public: DerivedA() { cout << "DerivedA:Constructor" <<endl; } ~DerivedA() { cout << "DerivedA:Destructor" << endl; } }; class DerivedB:public DerivedA { public: DerivedB() { cout << "DerivedB:Constructor" <<endl; } ~DerivedB() { cout << "DerivedB:Destructor" <<endl; } }; int main() { Base* tmp = new DerivedA(); delete tmp; return 0; }
#include<iostream> using namespace std; class Base { public: Base() { cout << "Base:Constructor" <<endl; } virtual ~Base() { cout << "Base:Destructor" << endl; } }; class DerivedA:public Base { public: DerivedA() { cout << "DerivedA:Constructor" <<endl; } ~DerivedA() { cout << "DerivedA:Destructor" << endl; } }; class DerivedB:public DerivedA { public: DerivedB() { cout << "DerivedB:Constructor" <<endl; } ~DerivedB() { cout << "DerivedB:Destructor" <<endl; } }; int main() { Base* tmp = new DerivedA(); delete tmp; return 0; }
原文:http://blog.csdn.net/haitunxiaomo/article/details/42061939