淺談C++基類的析構(gòu)函數(shù)為虛函數(shù)
1、原因:
在實(shí)現(xiàn)多態(tài)時(shí), 當(dāng)用基類指針操作派生類, 在析構(gòu)時(shí)候防止只析構(gòu)基類而不析構(gòu)派生類。
2、例子:
?。?)、
#include<iostream> using namespace std; class Base{ public: Base() {}; ~Base() {cout << "Output from the destructor of class Base!" << endl;}; void DoSomething() { cout << "Do something in class Base!" << endl; }; }; class Derived : public Base{ public: Derived() {}; ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; void DoSomething() { cout << "Do something in class Derived!" << endl; }; }; int main(){ Derived* p = new Derived; p->DoSomething(); delete p; return 0; }
運(yùn)行結(jié)果:
Do something in class Derived!
Output from the destructor of class Derived!
Output from the destructor of class Base!
代碼中基類的析構(gòu)函數(shù)不是虛函數(shù),在main函數(shù)中用繼承類的指針去操作繼承類的成員,釋放指針P的過程是:先釋放繼承類的資源,再釋放基類資源。
(2)、
#include<iostream> using namespace std; class Base{ public: Base() {}; ~Base() {cout << "Output from the destructor of class Base!" << endl;}; void DoSomething() { cout << "Do something in class Base!" << endl; }; }; class Derived : public Base{ public: Derived() {}; ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; void DoSomething() { cout << "Do something in class Derived!" << endl; }; }; int main(){ Base* p = new Derived; p->DoSomething(); delete p; return 0; }
運(yùn)行結(jié)果:
Do something in class ClxBase!
Output from the destructor of class ClxBase!
代碼中基類的析構(gòu)函數(shù)同樣不是虛函數(shù),不同的是在main函數(shù)中用基類的指針去操作繼承類的成員,釋放指針P的過程是:只釋放基類的資源,而沒有調(diào)用繼承類的析構(gòu)函數(shù)。 調(diào)用DoSomething()函數(shù)執(zhí)行的也是基類定義的函數(shù)。
一般情況下,這樣的刪除只能夠刪除基類對(duì)象,而不能刪除子類對(duì)象,形成了刪除一半形象,造成內(nèi)存泄漏。
在公有繼承中,基類對(duì)派生類及其對(duì)象的操作,只能影響到那些從基類繼承下來的成員。如果想要用基類對(duì)非繼承成員進(jìn)行操作,則要把基類的這個(gè)函數(shù)定義為虛函數(shù)。 析構(gòu)函數(shù)自然也應(yīng)該如此:如果它想析構(gòu)子類中的重新定義或新的成員及對(duì)象,當(dāng)然也應(yīng)該聲明為虛的。
?。?)、
#include<iostream> using namespace std; class Base{ public: Base() {}; virtual ~Base() {cout << "Output from the destructor of class Base!" << endl;}; virtual void DoSomething() { cout << "Do something in class Base!" << endl; }; }; class Derived : public Base{ public: Derived() {}; ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; void DoSomething() { cout << "Do something in class Derived!" << endl; }; }; int main(){ Base* p = new Derived; p->DoSomething(); delete p; return 0; }
運(yùn)行結(jié)果:
Do something in class ClxDerived!
Output from the destructor of class ClxDerived!
Output from the destructor of class ClxBase!
代碼中基類的析構(gòu)函數(shù)被定義為虛函數(shù),在main函數(shù)中用基類的指針去操作繼承類的成員,釋放指針P的過程是:釋放了繼承類的資源,再調(diào)用基類的析構(gòu)函數(shù)。調(diào)用DoSomething()函數(shù)執(zhí)行的也是繼承類定義的函數(shù)。
3、總結(jié):
基類指針可以指向派生類的對(duì)象(多態(tài)性),如果刪除該指針delete p;就會(huì)調(diào)用該指針指向的派生類析構(gòu)函數(shù),而派生類的析構(gòu)函數(shù)又自動(dòng)調(diào)用基類的析構(gòu)函數(shù),這樣整個(gè)派生類的對(duì)象完全被釋放。如果析構(gòu)函數(shù)不被聲明成虛函數(shù),則編譯器實(shí)施靜態(tài)綁定,在刪除基類指針時(shí),只會(huì)調(diào)用基類的析構(gòu)函數(shù)而不調(diào)用派生類析構(gòu)函數(shù),這樣就會(huì)造成派生類對(duì)象析構(gòu)不完全。所以,將析構(gòu)函數(shù)聲明為虛函數(shù)是十分必要的。
- C++超詳細(xì)講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實(shí)現(xiàn)
- C++深入講解對(duì)象的銷毀之析構(gòu)函數(shù)
- C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
- 詳解C++中虛析構(gòu)函數(shù)的作用及其原理分析
- 詳解C++ 編寫String 的構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)
- C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用順序詳解
- C++類成員構(gòu)造函數(shù)和析構(gòu)函數(shù)順序示例詳細(xì)講解
- C++ 私有析構(gòu)函數(shù)的作用示例詳解
相關(guān)文章
總結(jié)C/C++面試中可能會(huì)碰到的字符串指針題
C/C++是最能體現(xiàn)程序員能力的語言之一,其功能強(qiáng)大,在IT行業(yè)的各個(gè)方面都有大量的應(yīng)用。下面這篇文章主要介紹了總結(jié)了在C/C++面試中可能會(huì)碰到的字符串指針題,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01C語言經(jīng)典例程100例(經(jīng)典c程序100例)
這篇文章主要介紹了C語言經(jīng)典例程100例,經(jīng)典c程序100例,學(xué)習(xí)c語言的朋友可以參考一下2018-03-03C++實(shí)現(xiàn)對(duì)RGB圖片進(jìn)行編碼的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用得到的RGB信息重新對(duì)RGB圖片進(jìn)行編碼,以及對(duì)其他圖片如BMP所得到的RGB信息進(jìn)行編碼從而得到*.jpg文件,感興趣的可以了解一下2023-05-05window調(diào)用api列出當(dāng)前所有進(jìn)程示例
這篇文章主要介紹了window調(diào)用api列出當(dāng)前所有進(jìn)程示例,需要的朋友可以參考下2014-04-04Vs2019+Qt+Opencv環(huán)境配置心得(圖文)
這篇文章主要介紹了Vs2019+Qt+Opencv環(huán)境配置心得(圖文),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08