美文网首页
2019-06-14 C++ Destructor

2019-06-14 C++ Destructor

作者: memset | 来源:发表于2019-06-14 14:04 被阅读0次

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
    });

相关文章

网友评论

      本文标题:2019-06-14 C++ Destructor

      本文链接:https://www.haomeiwen.com/subject/dpnufctx.html