来个例子就明了了:
#include<iostream> using namespace std; class Base1 { public: virtual ~Base1(){ cout<<"Base1"<<endl; } }; class Base2 { public: virtual ~Base2(){ cout<<"Base2"<<endl; } }; class D1:public Base1{ public: ~D1() { cout<<"D1"<<endl; } }; class D2:public Base2{ public: ~D2() { cout<<"D2"<<endl; } }; class MI:public D1,public D2{ public: ~MI() { cout<<"MI"<<endl; } }; int main(int argc, char *argv[]) { Base1 *pb1=new MI; Base2 *pb2=new MI; D1 *pd1=new MI; D2 *pd2=new MI; delete pb2; cout<<endl; delete pd1; cout<<endl; delete pd2; cout<<endl; return 0; }
实质上它的结果和pd1的释放是一模一样的,虽然pd2是D2类型指针,但是析构会整个析构MI对象,这个对象包含了Base1子对象,D1子对象,Base2子对象,D2子对象,还有自身的部分。所有的过程为逆序析构,打印结果为:
原文地址:http://blog.csdn.net/wdkirchhoff/article/details/43492307