乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C 里面的虛析構(gòu)函數(shù)

       菌心說 2022-05-16 發(fā)布于北京

      C++的虛構(gòu)函數(shù)可以定義為虛函數(shù),這個(gè)在類需要繼承的時(shí)候是至關(guān)重要的。

      比如:

      #include <iostream>using namespace std;class Base {public: void hello() { cout << 'helloworld' << endl; }};class Derived: public Base {public: Derived(): Base() { } ~Derived() { cout << 'Derived destructor.' << endl; }};int main() { Base* pBase = new Derived(); pBase->hello(); delete pBase; return 0;}
      文章圖片1

      可以看到,Base class沒有定義析構(gòu)函數(shù)函數(shù),當(dāng)我們delete的時(shí)候使用Base的指針的時(shí),Derived類的析構(gòu)函數(shù)不會(huì)被調(diào)用。

      文章圖片2

      現(xiàn)在我們定義一個(gè)析構(gòu)函數(shù):

      class Base {public:    void hello() {        cout << 'helloworld' << endl;    }    ~Base() {        cout << 'Base destructor' << endl;    }};
      文章圖片3

      運(yùn)行結(jié)果如下,可以看到Base的析構(gòu)函數(shù)確實(shí)被調(diào)用了,但是Derived析構(gòu)函數(shù)還是沒有調(diào)用。

      文章圖片4

      最好我們把析構(gòu)函數(shù)申明為虛函數(shù):

      class Base {public: void hello() { cout << 'helloworld' << endl; } virtual ~Base() { cout << 'Base destructor' << endl; }};
      文章圖片5

      這次才真正把子類的析構(gòu)函數(shù)給調(diào)用了:

      文章圖片6

      所以在C++語言里面,如果沒有特殊原因,還是建議把析構(gòu)函數(shù)定義為虛函數(shù)。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多