C++实例之虚析构函数

来源:互联网 发布:周杰伦的影响力知乎 编辑:程序博客网 时间:2024/06/15 05:56
(一)父类的析构函数是虚函数时
// Test1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class ClxBase{public:    ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; };    virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };    virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public:    ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; };    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };     void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};/* 情形一// Output from the constructor of class ClxBase!// Do something in class ClxBase!// Output from the destructor of class ClxBase!int _tmain(int argc, _TCHAR* argv[]){ClxBase pTest;pTest.DoSomething();return 0;}*//* 情形二// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();return 0;}*//* 情形三// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!// Output from the destructor of class ClxBase!int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}*/

 

/* 情形四  // Output from the constructor of class ClxBase!  // Output from the constructor of class ClxDerived!  // Do something in class ClxDerived! // Output from the destructor of class ClxDerived!   // Output from the destructor of class ClxBase!   */ int _tmain(int argc, _TCHAR* argv[])  {      ClxDerived pTest;      pTest.DoSomething();      return 0;  }   



 

(二)父类的析构函数不是虚函数时
// Test2.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class ClxBase{public:    ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; };    ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; };    virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public:    ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; };    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };     void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};/* 情形三// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!*/int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}

 

可见如果父类的析构函数没有定义为虚函数的话,当用父类指针指向子类对象的时候,delete父类指针,只会调用父类中的析构函数,不会调用子类中的析构函数。