C++析構函數爲何要爲虛函數

注:本文內容來源於zhice163博文,感謝做者的整理。html

1.爲何基類的析構函數是虛函數?ios

  在實現多態時,當用基類操做派生類,在析構時防止只析構基類而不析構派生類的情況發生。網絡

  下面轉自網絡:源地址 http://blog.sina.com.cn/s/blog_7c773cc50100y9hz.html函數

  a.第一段代碼spa

  

複製代碼
複製代碼
#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl;};

    void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
    ClxDerived() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};
  int   main(){  
  ClxDerived *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  }
複製代碼
複製代碼

 

  運行結果:指針

  Do something in class ClxDerived!            code

  Output from the destructor of class ClxDerived!htm

  Output from the destructor of class ClxBase!  對象

  這段代碼中基類的析構函數不是虛函數,在main函數中用繼承類的指針去操做繼承類的成員,釋放指針P的過程是:先釋放繼承類的資源,再釋放基類資源. blog

 

  b.第二段代碼

  

複製代碼
複製代碼
#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    ~ClxBase() {cout << "Output from the destructor of class ClxBase!" << endl;};

    void DoSomething() { cout << "Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase{
public:
    ClxDerived() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; }
};
  int   main(){  
  ClxBase *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  } 
複製代碼
複製代碼

 

  輸出結果:

  Do something in class ClxBase!
  Output from the destructor of class ClxBase!

    這段代碼中基類的析構函數一樣不是虛函數,不一樣的是在main函數中用基類的指針去操做繼承類的成員,釋放指針P的過程是:只是釋放了基類的資源,而沒有調用繼承類的析構函數.調用  dosomething()函數執行的也是基類定義的函數.

    通常狀況下,這樣的刪除只可以刪除基類對象,而不能刪除子類對象,造成了刪除一半形象,形成內存泄漏.

    在公有繼承中,基類對派生類及其對象的操做,只能影響到那些從基類繼承下來的成員.若是想要用基類對非繼承成員進行操做,則要把基類的這個函數定義爲虛函數.

    析構函數天然也應該如此:若是它想析構子類中的從新定義或新的成員及對象,固然也應該聲明爲虛的. 

 

  c.第三段代碼:

  

複製代碼
複製代碼
#include<iostream>
using namespace std;
class ClxBase{
public:
    ClxBase() {};
    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() {};
    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };
    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };
};

  int   main(){  
  ClxBase *p =  new ClxDerived;
  p->DoSomething();
  delete p;
  return 0;
  }  
複製代碼
複製代碼

 

  運行結果:

  Do something in class ClxDerived!
  Output from the destructor of class ClxDerived!
  Output from the destructor of class ClxBase!

    這段代碼中基類的析構函數被定義爲虛函數,在main函數中用基類的指針去操做繼承類的成員,釋放指針P的過程是:只是釋放了繼承類的資源,再調用基類的析構函數.調用dosomething()函數執行的也是繼承類定義的函數.  

    若是不須要基類對派生類及對象進行操做,則不能定義虛函數,由於這樣會增長內存開銷.當類裏面有定義虛函數的時候,編譯器會給類添加一個虛函數表,裏面來存放虛函數指針,這樣就會增長類的存儲空間.因此,只有當一個類被用來做爲基類的時候,才把析構函數寫成虛函數.

相關文章
相關標籤/搜索