虛析構函數詳解
1.為什麽基類的析構函數是虛函數?
在實現多態時,當用基類操作派生類,在析構時防止只析構基類而不析構派生類的狀況發生。
下面轉自網絡:源地址 http://blog.sina.com.cn/s/blog_7c773cc50100y9hz.html
a.第一段代碼
#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!
Output from the destructor of class ClxDerived!
Output from the destructor of class ClxBase!
這段代碼中基類的析構函數不是虛函數,在main函數中用繼承類的指針去操作繼承類的成員,釋放指針P的過程是:先釋放繼承類的資源,再釋放基類資源.
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()函數執行的也是繼承類定義的函數.
如果不需要基類對派生類及對象進行操作,則不能定義虛函數,因為這樣會增加內存開銷.當類裏面有定義虛函數的時候,編譯器會給類添加一個虛函數表,裏面來存放虛函數指針,這樣就會增加類的存儲空間.所以,只有當一個類被用來作為基類的時候,才把析構函數寫成虛函數.
虛析構函數詳解