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 dump_a = this->a;
this->b = std::shared_ptr(this->a->CreateB(),
    [this, dump_a](B* p)
    {
        dump_a->ReleaseB(p); // OK
        this->a->ReleaseB(p); // Error
    });

你可能感兴趣的:(2019-06-14 C++ Destructor)