#include<iostream> using namespace std; class a{ public: a(){cout<<"a!"<<endl;} virtual void disp(){cout<<"a::disp()!"<<endl;} virtual ~a(){cout<<"~a!"<<endl;} }; class b:public a{ public: b(){cout<<"b!"<<endl;} ~b(){cout<<"~b!"<<endl;} }; class c:public b{ public: c(){cout<<"c!"<<endl;} void disp(){cout<<"c::disp()!"<<endl;} ~c(){cout<<"~c!"<<endl;} }; int main() { a*p=new c(); p->disp(); delete p; return 0; } $ ./a.out a! b! c! c::disp()! ~c! ~b! ~a! 构造函数从基类到派生类,析构函数相反 虚函数调用在运行时确定具体对象的函数; 若基类成员函数a.disp()和析构函数~a()前没有virtual关键字,输出为: $ ./a.out a! b! c! a::disp()! ~a!