对象的生命周期结束,析构函数被调用之前? [英] Lifetime of object is over before destructor is called?

查看:206
本文介绍了对象的生命周期结束,析构函数被调用之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这一点:


3.8 / 1类型T的对象的生命周期结束于: - 如果T是具有非平凡析构函数(12.4)的类类型,析构函数调用
开始,或者 - 对象占用的存储器被重用或释放
p>

3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or — the storage which the object occupies is reused or released."

如果生命周期在析构函数开始之前结束,那不是说访问析构函数中的成员是未定义的行为?

我也看到这个报价:


12.7一个非平凡的析构函数,在析构函数
完成执行后引用对象的任何非静态成员或基类,会导致未定义的行为。

12.7 "For an object with a non-trivial destructor, referring to any non-static member or base class of the object after the destructor finishes execution results in undefined behavior."


推荐答案

生命周期对象与对象的消费者相关,而不是对象本身。因此,一旦销毁开始,消费类不应该尝试访问对象的成员。

The "lifetime" of an object is relevant for consumers of the object, not the object itself. Therefore a consuming class should not attempt to access members of an object once destruction has started.

这篇关于对象的生命周期结束,析构函数被调用之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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