為什麼解構函式可以為虛函數,什麼情況下需要將解構函式定義為虛函數?

來源:互聯網
上載者:User

首先要明確:

1.每個解構函式(不加 virtual) 只負責清除自己的成員。
2.可能有基類指標,指向的確是衍生類別成員的情況。(這是很正常的),
   那麼當析構一個指向衍生類別成員的基類指標時,程式就不知道怎麼辦了。
   所以要保證運行適當的解構函式,基類中的解構函式必須為虛析構。

        基類指標可以指向衍生類別的對象(多態性),如果刪除該指標delete []p;就會調用該指標指向的衍生類別解構函式,而衍生類別的解構函式又自動調用基類的解構函式,這樣整個衍生類別的對象完全被釋放。如果解構函式不被聲明成虛函數,則編譯器實施靜態繫結,在刪除基類指標時,只會調用基類的解構函式而不調用衍生類別解構函式,這樣就會造成衍生類別對象析構不完全。所以,將解構函式聲明為虛函數是十分必要的。

 

範例程式碼:

1.第一段代碼

#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的過程是:先釋放繼承類的資源,再釋放基類資源.

2.第二段代碼

#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()函數執行的也是基類定義的函數.

一般情況下,這樣的刪除只能夠刪除基類對象,而不能刪除子類對象,形成了刪除一半形象,造成記憶體流失.

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

解構函式自然也應該如此:如果它想析構子類中的重新定義或新的成員及對象,當然也應該聲明為虛的.

3.第三段代碼:

#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()函數執行的也是繼承類定義的函數.

如果不需要基類對衍生類別及對象進行操作,則不能定義虛函數,因為這樣會增加記憶體開銷.當類裡面有定義虛函數的時候,編譯器會給類添加一個虛函數表,裡面來存放虛函數指標,這樣就會增加類的儲存空間.所以,只有當一個類被用來作為基類的時候,才把解構函式寫成虛函數.

 

內容來自:

http://zhidao.baidu.com/question/207712177.html

http://blog.sina.com.cn/s/blog_7c773cc50100y9hz.html

       轉過來是為了做一個筆記,方便自己以後查看這個知識點。感謝以上連結處的作者們!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.