析构函数调用顺序的原因reqd。 [英] Reason reqd for the order of destructor call .?

查看:111
本文介绍了析构函数调用顺序的原因reqd。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在某些论坛上所读到的那样,当派生类对象被创建基类成员并且方法在内存中分配了空间,但是没有特定的基类对象时。

As I have read in certain forums,when derived class object is created base class members and methods are allocated space in the memory but there is no specific base class object.

现在,当派生类对象超出范围时,为什么要首先调用派生类析构函数。在基类析构函数之后不能调用派生类析构函数的编译器的约束是什么??

Now as the derived class object goes out of scope , why derived class destructor is called first.What is the constraint of the compiler where derived class destructor cannot be called after base class destructor..?

如果我对理解有误,请更正我。.谢谢

Please correct me in case I have built a wrong understanding..Thanks in advance

推荐答案

创建派生类对象后,就有一个 特定的基类对象(实际上是子对象)。即,当您创建派生对象时,基类ctor用于初始化派生对象中的基类subj-object,只有在完成后,派生类ctor才开始做它的事情,初始化添加到对象中的所有成员。

When a derived class object is created, there is a specific base-class object (sub-object, really). I.e., when you create a derived object, a base class ctor is used to initialize the base class subj-object within the derived object, and only after that completes does the derived class ctor get to do its thing, initialing any members added in the derived class, etc.

由于它是从基础构建到派生的,因此已从派生分解为基础。当派生的dtor结束执行时,仍然应该有一个完全有效的基础对象,等待基础dtor销毁它。但是,如果您首先尝试销毁基础子对象,则在运行派生dtor时,您将不再具有要销毁的dtor销毁的有效派生对象。

Since it's built from base to derived, it's torn down from derived to base. When the derived dtor ends execution, there should still be a perfectly valid base object waiting for the base dtor to destroy it. If you tried to destroy the base sub-object first, however, when you ran the derived dtor, you'd no longer have a valid derived object for the derived dtor to destroy.

这篇关于析构函数调用顺序的原因reqd。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆