雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

1、原因:

   在實現(xiàn)多態(tài)時, 當(dāng)用基類指針操作派生類, 在析構(gòu)時候防止只析構(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;
   }

  運行結(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的過程是:先釋放繼承類的資源,再釋放基類資源。

 ?。?)、

   #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;
   }

    運行結(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)存泄漏。

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

  運行結(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ù)是十分必要的。

上一篇:VC++ 使用 _access函數(shù)判斷文件或文件夾是否存在

欄    目:C語言

下一篇:MFC實現(xiàn)在文件尾追加數(shù)據(jù)的方法

本文標(biāo)題:淺談C++基類的析構(gòu)函數(shù)為虛函數(shù)

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2674.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有