欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ virtual destructor虛擬析構(gòu)函數(shù)

 更新時(shí)間:2021年05月24日 09:24:48   作者:我是小白呀  
C++中基類采用virtual虛析構(gòu)函數(shù)是為了防止內(nèi)存泄漏。具體地說(shuō),如果派生類中申請(qǐng)了內(nèi)存空間,并在其析構(gòu)函數(shù)中對(duì)這些內(nèi)存空間進(jìn)行釋放,今天通過(guò)本文給大家介紹C++ virtual destructor虛擬析構(gòu)函數(shù)的相關(guān)知識(shí),感興趣的朋友一起看看吧

概述

虛析構(gòu)函數(shù) (virtual destructor) 可以幫我們實(shí)現(xiàn)基類指針刪除派生類對(duì)象.

在這里插入圖片描述

問(wèn)題

當(dāng)我們從派生類的對(duì)象從內(nèi)存中撤銷時(shí)會(huì)先調(diào)用派生的析構(gòu)函數(shù), 然后再基類的析構(gòu)函數(shù), 由此就會(huì)產(chǎn)生問(wèn)題:

  • 如果用 new 運(yùn)算符建立了派生類對(duì)象, 并且由一個(gè)基類的指針比那里指向該對(duì)象
  • 用 delete 運(yùn)算符撤銷對(duì)象時(shí), 系統(tǒng)只執(zhí)行基類的析構(gòu)函數(shù). 而不執(zhí)行派生類的析構(gòu)函數(shù), 派生類對(duì)象析構(gòu)中要求的工作將被忽略

Base 類:

#ifndef PROJECT6_BASE_H
#define PROJECT6_BASE_H

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "執(zhí)行基類構(gòu)造函數(shù)" << endl;
    };
    ~Base() {
        cout << "執(zhí)行基類析構(gòu)函數(shù)" << endl;
    };
};

#endif //PROJECT6_BASE_H

Derived 類:

#ifndef PROJECT6_DERIVED_H
#define PROJECT6_DERIVED_H

#include <iostream>
#include "Base.h"
using namespace std;

class Derived : public Base {
public:
    Derived() {
        cout << "執(zhí)行派生類構(gòu)造函數(shù)" << endl;
    };
    ~Derived() {
        cout << "執(zhí)行派生類析構(gòu)函數(shù)" << endl;
    }
};

#endif //PROJECT6_DERIVED_H

main:

#include <iostream>
#include "Derived.h"
using namespace std;

int main() {

    Base *pt =new Derived;
    delete pt;

    return 0;
}

輸出結(jié)果:

執(zhí)行基類構(gòu)造函數(shù)
執(zhí)行派生類構(gòu)造函數(shù)
執(zhí)行基類析構(gòu)函數(shù)

虛析構(gòu)函數(shù)

當(dāng)基類的析構(gòu)函數(shù)為虛函數(shù)時(shí), 無(wú)論指針指的是同一族中的哪一個(gè)類對(duì)象, 系統(tǒng)會(huì)采用動(dòng)態(tài)關(guān)聯(lián), 掉啊用相應(yīng)的析構(gòu)函數(shù), 對(duì)該對(duì)象進(jìn)行清理工作. 即先調(diào)用了派生類的析構(gòu)函數(shù), 再調(diào)用了基類的析構(gòu)函數(shù).

Base 類:

#ifndef PROJECT6_BASE_H
#define PROJECT6_BASE_H

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "執(zhí)行基類構(gòu)造函數(shù)" << endl;
    };
    virtual ~Base() {
        cout << "執(zhí)行基類析構(gòu)函數(shù)" << endl;
    };
};

#endif //PROJECT6_BASE_H

Derived 類:

#ifndef PROJECT6_DERIVED_H
#define PROJECT6_DERIVED_H

#include <iostream>
#include "Base.h"
using namespace std;

class Derived : public Base {
public:
    Derived() {
        cout << "執(zhí)行派生類構(gòu)造函數(shù)" << endl;
    };
    ~Derived() {
        cout << "執(zhí)行派生類析構(gòu)函數(shù)" << endl;
    }
};

#endif //PROJECT6_DERIVED_H

main:

#include <iostream>
#include "Derived.h"
using namespace std;

int main() {

    Base *pt =new Derived;
    delete pt;

    return 0;
}

輸出結(jié)果:

執(zhí)行基類構(gòu)造函數(shù)
執(zhí)行派生類構(gòu)造函數(shù)
執(zhí)行派生類析構(gòu)函數(shù)
執(zhí)行基類析構(gòu)函數(shù)

總結(jié)

如果將基類的析構(gòu)函數(shù)聲明為虛函數(shù)時(shí), 由該基類所派生的所有派生類的析構(gòu)函數(shù)也都自動(dòng)成為虛函數(shù). 即使派生類的析構(gòu)函數(shù)與其基類的構(gòu)造函數(shù)名字不相同.

最好把基類的析構(gòu)函數(shù)聲明為虛函數(shù). 即使基類并不需要析構(gòu)函數(shù), 我們也可以定義一個(gè)函數(shù)體為空的虛析構(gòu)函數(shù), 以保證撤銷動(dòng)態(tài)分配空間能正確的處理.

注: 構(gòu)造函數(shù)不能聲明為虛函數(shù).

以上就是C++ virtual destructor虛擬析構(gòu)函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于C++虛擬析構(gòu)函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論