C ++析构函数:何时释放内存? [英] C++ destructor: when the memory gets freed?

查看:87
本文介绍了C ++析构函数:何时释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我删除了一个导致其析构函数被调用的对象,那么在析构函数完成函数中的所有操作之前或之后,内存是否将被释放?

If I delete an object which causes its destructor to be called, does the memory get freed before or after the destructor has finished doing whatever there is in the function?

推荐答案

仅在最小派生类子对象被销毁后才释放内存.因此,如果您有:

Memory is only freed once the least derived class subobject has been destroyed. So if you have:

class Base {
};

class Derived : public Base {
public:
    ~Derived();
};

然后先销毁 Derived ,然后销毁 Base ,然后才释放内存.

then first Derived is destroyed, then Base is destroyed and only then memory is deallocated.

这篇关于C ++析构函数:何时释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆