简体   繁体   English

在多级继承中调用析构函数(c ++)

[英]Call of the destructors in multilevel inheritance (c++)

I coded the following classes in order to test the multi-level inheritance concept. 我编写了以下类来测试多级继承概念。 There is a point that I didn't really understand when I was trying to test the calls to constructors and destructors. 当我试图测试对构造函数和析构函数的调用时,有一点我真的不明白。


#include <iostream>

using namespace std;

class X{

    public:
        X(){cout <<"Construct X " << endl;};
        virtual ~X(){cout <<"Destruct X " << endl;};
        virtual void print() const = 0;
};

class Y: public X{
    public:
        Y(){cout <<"construct Y " << endl;};
        ~Y(){cout <<"Destruct Y " << endl;};
        void print() const{
            cout <<"print Y" << endl;
        };
};

class Z: public Y{
    public:
        Z(){cout <<"Construct Z" << endl; };
        ~Z(){cout <<"Destruct Z " << endl; };
        void print() const{
            cout <<" Print Z" << endl;
        };
};

int main()
{
    Y y;
    //Why isn't Y being destructed in here
    Z z;
    return 0;
}

Output 产量

The output is the following. 输出如下。 I understood that we start from the base class . 我明白我们从基类开始。 So in Y y; 所以在Y y; first the constructor of X is called, then Y . 首先调用X构造函数 ,然后调用Y. In Z z; Z z; first the construct of X is called, then Y and finally Z . 首先调用X构造 ,然后调用Y ,最后调用Z.

Construct X
construct Y
Construct X
construct Y
Construct Z
Destruct Z
Destruct Y
Destruct X
Destruct Y
Destruct X

Question

  • Why isn't the destructor for Y being called right after Y y; 为什么在Y y;之后不能正确调用Y的析构函数Y y; . Why should we wait till Z is constructed then call the destructors. 我们为什么要等到Z构建然后调用析构函数。 Meaning why doesn't the output look like that: 意思是为什么输出看起来不像那样:

     Construct X construct Y Destruct Y Destruct X Construct X construct Y Construct Z Destruct Z Destruct Y Destruct X 

Inheritance is a red herring here. 继承在这里是一个红鲱鱼。 It's not relevant. 这不相关。

y and z have automatic storage duration and are required, in this case, to stay in scope until the closing brace of the function. yz具有自动存储持续时间 ,在这种情况下,需要保持在范围内直到函数的右括号。

And z will be destructed before y . 并且z y 之前被破坏。 (Automatic variables go out of scope in the reverse order in which they were created, all other things being equal.) (自动变量按照创建它们的相反顺序超出范围,所有其他条件相同。)

This is unrelated to inheritance. 这与继承无关。 Variables declared in block scope are destroyed when that block is left, ie a return statement is taken or the final } is reached or an exception is thrown. 在块范围内声明的变量在剩下该块时被销毁,即获取return语句或达到final }或抛出异常。

The objects exist at the end of their scope, and the objects get destroyed in reverse order of their construction. 对象存在于其范围的末尾,并且对象以其构造的相反顺序被销毁。

y is constructed first, then z . 首先构造y ,然后构造z So, when their mutual scope's end is reached, z gets destroyed first, then y . 因此,当达到他们的共同范围的结束时, z首先被破坏,然后是y

You were expecting the objects to be destroyed in the same order they were constructed, but they actually get destroyed in reverse order of construction. 你期望对象以它们构造的相同顺序被销毁,但它们实际上会以相反的构造顺序被销毁。

Similarly, when an object gets constructed, its base class is constructed first, then the derived class. 类似地,当构造一个对象时,首先构造它的基类,然后构造派生类。 When the object gets destroyed, the derived class gets destroyed first, then the base class. 当对象被销毁时,派生类首先被销毁,然后是基类。

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

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