国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

淺談C++基類的析構(gòu)函數(shù)為虛函數(shù)

2020-05-23 14:14:02
字體:
供稿:網(wǎng)友

本文重點:應(yīng)該為多態(tài)基類聲明虛析構(gòu)器。一旦一個類包含虛函數(shù),它就應(yīng)該包含一個虛析構(gòu)器。如果一個類不用作基類或者不需具有多態(tài)性,便不應(yīng)該為它聲明虛析構(gòu)器。

1、原因:

在實現(xiàn)多態(tài)時, 當(dāng)用基類指針操作派生類, 在析構(gòu)時候防止只析構(gòu)基類而不析構(gòu)派生類。

2、例子:

(1)、

 

  1. #include<iostream> 
  2. using namespace std; 
  3.  
  4. class Base{ 
  5. public
  6.    Base() {}; 
  7. ~Base() {cout << "Output from the destructor of class Base!" << endl;}; 
  8.  
  9. void DoSomething() { cout << "Do something in class Base!" << endl; }; 
  10. }; 
  11.  
  12. class Derived : public Base{ 
  13. public
  14.    Derived() {}; 
  15. ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; 
  16.  
  17. void DoSomething() { cout << "Do something in class Derived!" << endl; }; 
  18. }; 
  19. int main(){  
  20.    Derived* p = new Derived; 
  21.    p->DoSomething(); 
  22.    delete p; 
  23.    return 0; 

運行結(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)、

 

  1. #include<iostream> 
  2. using namespace std; 
  3.  
  4. class Base{ 
  5. public
  6.    Base() {}; 
  7. ~Base() {cout << "Output from the destructor of class Base!" << endl;}; 
  8.  
  9. void DoSomething() { cout << "Do something in class Base!" << endl; }; 
  10. }; 
  11.  
  12. class Derived : public Base{ 
  13. public
  14.    Derived() {}; 
  15. ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; 
  16.  
  17. void DoSomething() { cout << "Do something in class Derived!" << endl; }; 
  18. }; 
  19. int main(){  
  20.    Base* p = new Derived; 
  21.    p->DoSomething(); 
  22.    delete p; 
  23.    return 0; 

運行結(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ù)。

一般情況下,這樣的刪除只能夠刪除基類對象,而不能刪除子類對象,形成了刪除一半形象,造成內(nèi)存泄漏。

在公有繼承中,基類對派生類及其對象的操作,只能影響到那些從基類繼承下來的成員。如果想要用基類對非繼承成員進行操作,則要把基類的這個函數(shù)定義為虛函數(shù)。 析構(gòu)函數(shù)自然也應(yīng)該如此:如果它想析構(gòu)子類中的重新定義或新的成員及對象,當(dāng)然也應(yīng)該聲明為虛的。

(3)、

 

 
  1. #include<iostream> 
  2. using namespace std; 
  3.  
  4. class Base{ 
  5. public
  6.    Base() {}; 
  7. virtual ~Base() {cout << "Output from the destructor of class Base!" << endl;}; 
  8.  
  9. virtual void DoSomething() { cout << "Do something in class Base!" << endl; }; 
  10. }; 
  11.  
  12. class Derived : public Base{ 
  13. public
  14.    Derived() {}; 
  15. ~Derived() { cout << "Output from the destructor of class Derived!" << endl; }; 
  16.  
  17. void DoSomething() { cout << "Do something in class Derived!" << endl; }; 
  18. }; 
  19. int main(){  
  20.    Base* p = new Derived; 
  21.    p->DoSomething(); 
  22.    delete p; 
  23.    return 0; 

運行結(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é):

基類指針可以指向派生類的對象(多態(tài)性),如果刪除該指針delete p;就會調(diào)用該指針指向的派生類析構(gòu)函數(shù),而派生類的析構(gòu)函數(shù)又自動調(diào)用基類的析構(gòu)函數(shù),這樣整個派生類的對象完全被釋放。如果析構(gòu)函數(shù)不被聲明成虛函數(shù),則編譯器實施靜態(tài)綁定,在刪除基類指針時,只會調(diào)用基類的析構(gòu)函數(shù)而不調(diào)用派生類析構(gòu)函數(shù),這樣就會造成派生類對象析構(gòu)不完全。所以,將析構(gòu)函數(shù)聲明為虛函數(shù)是十分必要的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 通江县| 阿图什市| 招远市| 静乐县| 兴城市| 广灵县| 西藏| 平和县| 铜陵市| 黄龙县| 嘉祥县| 响水县| 六安市| 舒城县| 阿巴嘎旗| 江山市| 科技| 岑巩县| 隆安县| 阜阳市| 竹山县| 玉屏| 留坝县| 峨山| 镇江市| 黄浦区| 丰台区| 宁都县| 天等县| 子长县| 和田县| 三原县| 简阳市| 东辽县| 万荣县| 雷山县| 翁牛特旗| 淮南市| 阿坝县| 阿坝县| 清远市|