泠叶967 2021-10-07 15:40 采纳率: 50%
浏览 43
已结题

问一个关于C++多态中析构函数调用的问题

在跟着B站某视频学C++学到多态时敲代码,发现一个问题,先上代码(使用的是Visual Studio)

#include<iostream>
using namespace std;
#include<string>

class cpu {
public:
    virtual void calculate() = 0;
};
class card {
public:
    virtual void display() = 0;
};
class memory {
public:
    virtual void store() = 0;
};

class IntelCPU :public cpu {
public:
    void calculate() {
        cout << "使用IntelCPU计算" << endl;
    }
    ~IntelCPU() {
        cout << "调用IntelCPU析构函数" << endl;
    }
};
class IntelCARD :public card {
public:
    void display() {
        cout << "使用IntelCARD显示" << endl;
    }
    ~IntelCARD() {
        cout << "调用IntelCARD析构函数" << endl;
    }
};
class IntelMEM :public memory {
    void store() {
        cout << "使用IntelMEM存储" << endl;
    }
};

class computer {
public:
    computer(cpu* cp, card* ca, memory* me) {
        a = cp;
        b = ca;
        c = me;
    }
    void work() {
        a->calculate();
        b->display();
        c->store();
    }
    ~computer() {
        if (a != NULL) {
            delete a;
            a = NULL;
        }
        if (b != NULL) {
            delete b;
            b = NULL;
        }
        if (c != NULL) {
            delete c;
            c = NULL;
        }
        cout << "调用computer析构函数" << endl;
    }
private:
    cpu* a;
    card* b;
    memory* c;
};

void dowork(computer* com) {
    com->work();
}
int main() {
    IntelCPU* cp= new IntelCPU;
    IntelCARD* ca = new IntelCARD;
    IntelMEM* me = new IntelMEM;
    computer* a = new computer(cp, ca, me);
    dowork(a);
    delete a;
}

结果:

img

在main函数中,delete a后cp开辟的空间确实被释放了(在computer a的析构函数中释放),但此时却没有调用IntelCPU的析构函数:

class IntelCPU :public cpu {
public:
    void calculate() {
        cout << "使用IntelCPU计算" << endl;
    }
    ~IntelCPU() {
        cout << "调用IntelCPU析构函数" << endl;
    }
};

而如果删去delete a,而加上delete cp,则会调用该析构函数:

img

但是如果同时写上:delete adelete cp,此时编译器则会报错:

img

我估计原因是因为重复释放了内存,也就证明前面cp开辟的空间确实被释放了,但此时在computer中的析构函数:

    ~computer() {
        if (a != NULL) {
            delete a;
            a = NULL;
        }
        if (b != NULL) {
            delete b;
            b = NULL;
        }
        if (c != NULL) {
            delete c;
            c = NULL;
        }
        cout << "调用computer析构函数" << endl;
    }

中的delete a(就是delete cp,,写的没注意名称重复的问题。。)却并不会调用IntelCPU的析构函数。
想问问各位对此有什么看法?
(重点不是在cp已经被释放,而是没有调用析构函数,如果我IntelCPI* cp时,cp中有指针开辟了堆上数据,那不是这部分数据就不能释放而泄漏了吗?
ps:视频中还教了一种写法,即:computer* a = new computer(new IntelCPU,new IntelCARD,new IntelMEM);,此时就完全不能调用各个Intel的析构函数了,因为在main中连类型名都没有);

  • 写回答

2条回答 默认 最新

  • panfei625 2021-10-07 16:03
    关注

    你这是学的那个老是忘记STRING头文件的黑马程序员的例子吧,我也写过一个,刚才去试了下,删除computer类的时候也是没有调用intelcpu的析构函数。我也怀疑数据没有释放,但是你说----delete a后cp开辟的空间确实被释放了(在computer a的析构函数中释放),这是根据什么来的???

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月7日

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块