C++解構函式為什麼要為虛擬函式
在實現多型時,當用基類操作派生類,在析構時防止只析構基類而不析構派生類的狀況發生。
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()函式執行的也是繼承類定義的函式.
如果不需要基類對派生類及物件進行操作,則不能定義虛擬函式,因為這樣會增加記憶體開銷.當類裡面有定義虛擬函式的時候,編譯器會給類新增一個虛擬函式表,裡面來存放虛擬函式指標,這樣就會增加類的儲存空間.所以,只有當一個類被用來作為基類的時候,才把解構函式寫成虛擬函式.