简体   繁体   English

我需要知道编译器如何与 cpp 中的析构函数一起工作

[英]i need to know how compiler work with destructors in cpp

Lets say I have 2 diffrent classes.假设我有 2 个不同的课程。

Why when I put class A object as data member inside class B , the class that getting destroyed first is class B and then class A but when i put class A object in the builder of class B then class A getting destroyed first and then class B ?为什么当我将A类对象作为数据成员放入B类时,首先销毁的类是B类,然后是B类,但是当我将A类对象放入B类的构建器中时, BA被销毁,然后B类? for example:例如:

class A {
public:
    ~A() { std::cout << "Deleting A\n"; }
};

class B {
private:
    A object;
public:
    ~B() { std::cout << "Deleting B\n"; }
};

int main( void ) {
    B test;
    return 0;
}

result:结果:

Deleting B删除 B
Deleting A删除A

But the next example gives me the opposite result :但下一个例子给了我相反的结果:

class A {
public:
    ~A() { std::cout << "Deleting A\n"; }
};

class B {
public:
    B() { A object; }
    ~B() { std::cout << "Deleting B\n"; }
};

int main( void ) {
    B test;
    return 0;
}

result:结果:

Deleting A删除A
Deleting B删除 B

In your second example B test;在您的第二个示例B test; calls the constructor of B.调用 B 的构造函数。

The constructor B is just a function and it has a local variable A object;构造函数 B 只是一个函数,它有一个局部变量A object; . . This object is created and at the end of the scope (end of the constructor of B) it is detroyed.这个对象被创建并在作用域的末尾(B 的构造函数的末尾)它被销毁。

Back in main at the end of main the object test is destroyed.在 main 结束时返回 main 对象test被销毁。

So in that case the object A object;所以在那种情况下,对象是A object; is destroyed first, later object B test;先销毁,后对象B test; is destroyed.被摧毁。

In your first example this is something different.在你的第一个例子中,这是不同的。 There A object;A object; is a member of class B, so the constructor of B is constructing the member automatically.是类 B 的成员,因此 B 的构造函数正在自动构造该成员。 The order of construction is: base classes first (not present in your example), then members, then the body of the constructor.构造的顺序是:首先是基类(在您的示例中不存在),然后是成员,然后是构造函数的主体。 Destruction is the opposite order: body of the destructor first, then the members (including your A object; ), then the base classes (not present in your example).析构是相反的顺序:首先是析构函数的主体,然后是成员(包括您的A object; ),然后是基类(在您的示例中不存在)。

As already said in a comment, you can easily step through the code in a debugger to get a better understanding of the order of execution.正如评论中已经说过的那样,您可以轻松地在调试器中单步执行代码,以更好地理解执行顺序。

因为类 A 的对象在类 B 的构造函数完成后立即销毁(因为它是函数内部的局部变量),并且在调用类 B 的析构函数之前。

In the second example you've got your object (instance of Class A), local in your Constructor.在第二个例子中,你已经得到了你的对象(类的实例),在构造函数本地 When you now instantiate B in your main function, the constructor is called, creates object , and destroys it, as sonn as the constructor has finished.当您现在在主函数中实例化 B 时,构造函数被调用,创建object并销毁它,就像构造函数完成一样。 In your first example, object is a class member and lives as long as your class object in your main function.在您的第一个示例中, object是一个类成员,并且与您的 main 函数中的类对象一样长。 When the programm is finished it calls your costum Destructor , this is then executed first, then it destructs all members ( standard Constructor ).程序完成后,它会调用您的costum Destructor ,然后首先执行它,然后它会破坏所有成员(标准 Constructor )。

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

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