简体   繁体   English

关于析构函数调用的困惑

[英]Confusion about destructor's calling

Does a destructor gets called before an object is destroyed or after an object is destroyed?在 object 被销毁之前或在 object 被销毁之后,是否调用了析构函数? I think that it is called before an object is destroyed because after an object is destroyed we cant access that memory to free any resource in there but if I am wrong then please correct me so I can understand it well我认为它是在 object 被破坏之前调用的,因为在 object 被破坏之后,我们无法访问 memory 以释放其中的任何资源,但如果我错了,请纠正我,以便我可以理解

#include <iostream>
#include <cassert>
#include <cstddef>
class Check
{
public:
    int neu{};
    Check() = default;
    Check(int n)
    {
        neu = n;
    }
    void print()
    {
        std::cout << neu << std::endl;
    }
    ~Check()
    {
        std::cout << "It has been destroyed "<<neu <<std::endl;
    }
};
int main()
{
    Check let,see(30);
    let.print();
    return 0;
    // does destructor gets called here
} // or does destructor gets called herecode here

Does a destructor gets called before an object is destroyed or after an object is destroyed?在 object 被销毁之前或在 object 被销毁之后,是否调用了析构函数?

The lifetime of the object has ended when the destructor is called.当调用析构函数时,object 的生命周期已经结束。 The object is destroyed by the destructor. object 被析构函数破坏。

    // does destructor gets called here
} // or does destructor gets called herecode here

There's really no practical difference.真的没有什么实际的区别。

The automatic objects are alive within the block and they aren't alive outside the block.自动对象在块内是活动的,它们在块外是不活动的。 The objects are destroyed when the block is exited.退出块时,对象将被销毁。 As such, it is reasonable to say that they are destroyed at the point where the closing brace is.因此,可以合理地说它们在右大括号所在的位置被销毁。

From ISO standard ( Special member functions - Destructors ):来自 ISO 标准(特殊成员函数-析构函数):

Once a destructor is invoked for an object, the object no longer exists;一旦为 object 调用析构函数,object 就不再存在; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended.如果为生命周期已结束的 object 调用析构函数,则行为未定义。

Destructor call is essentially description of actions need to consider object destroyed.析构函数调用本质上是描述需要考虑object被破坏的动作。 Destructor can be called explicitly, it results in end of life for that object.可以显式调用析构函数,它会导致 object 的生命周期结束。 Explicit calls are rare, usually done for objects created by placement new.显式调用很少见,通常用于放置 new 创建的对象。

Given the example provided, the question is actually about order of destructor calls.鉴于提供的示例,问题实际上是关于析构函数调用的顺序。 The destructors for sub-objects ( base class-type included, if any present) are called after.之后调用子对象的析构函数(包括基类类型,如果有的话)。 So you can safely access them until exit from destructor.因此,您可以安全地访问它们,直到从析构函数中退出。 Order of call is based on declaration order, just like order of initialization but inverted.调用顺序是基于声明顺序的,就像初始化的顺序一样,但是倒过来了。 Subobject of base-class type is considered the first for initialization and the last for destruction.基类类型的子对象被认为是第一个初始化和最后一个销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM