C ++中的构造函数和析构函数 [英] consructor and destructor in c++

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

问题描述



如果我们创建类的对象,则它首先调用基类构造函数,然后再调用派生类构造函数.在使用析构函数的情况下,它将调用派生类的析构函数,然后进行基类调用.你能告诉我原因吗?

在此先感谢



If we create an object of a class it first calls the base class constructor and then the derived class constructor. In the case of distructor it calls derived class''s destructor and then base. Can you please tell me the reason.

Thanks in advance

推荐答案

因为派生的构造函数可能希望已经建立了由基础设置的事物,并且在删除时,派生的类会优先基本上出于相同的原因,以关闭物体而射击.
Because the derived constructor could expect things to have been set up that are set up by the base, and when you delete, the derived class gets first shot at shutting the object down for basically the same reason in reverse.


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

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