构造函数和析构函数调用 [英] Constructor and destructor Calls

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

问题描述

当我们创建一个派生类的对象时,为什么构造函数以自上而下的方式调用(首先是基构造函数,然后是派生构造函数),而析构函数被称为自下而上(首先是派生构造函数,然后是基构造函数)

When we create an object of a derived class, why are the constructors called in a top-to-bottom fashion (first the base constructor, then derived constructor) and are destructors called bottom-to-top (first derived constructor, then base constructor)

推荐答案

为了向新手解释,假设您正在建造一座新建筑.

To explain for newbies, consider you are constructing a new building.

你建造地下室,一楼,二楼.摧毁 时,你摧毁 2 层、1 层和地下室.

You Construct basement, first floor, 2nd floor. While destroying , you destroy 2nd Floor, First Floor, and basement.

以同样的方式,对象的构造/销毁发生在 C++ 中.

The same way, construction/destruction of a object happens in C++.

希望这有助于理解.

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

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