2019-06-14 C++ Destructor

For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct non-virtual base classes in reverse order of construction (which in turn call the destructors of their members and their base classes, etc), and then, if this object is of most-derived class, it calls the destructors of all virtual bases.

在Destructor调用完成后,成员的Destructor调用前,this的状态是这样的:


image.png

如果在析构成员B时,使用了成员A,那么需要手动获取A的指针,而不是使用this捕获。因为这时的this已经是失效的了:

std::shared_ptr<A> dump_a = this->a;
this->b = std::shared_ptr<B>(this->a->CreateB(),
    [this, dump_a](B* p)
    {
        dump_a->ReleaseB(p); // OK
        this->a->ReleaseB(p); // Error
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容