gpt4 book ai didi

c++ - 在调用析构函数之前对象的生命周期结束了吗?

转载 作者:可可西里 更新时间:2023-11-01 14:59:33 27 4
gpt4 key购买 nike

我不明白这个:

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 "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."

但它并没有明确说明在析构函数期间允许做什么。

最佳答案

If the lifetime ends before the destructor starts, doesn't that mean accessing members in the destructor is undefined behavior?

希望不会:

来自N3242 构造与破坏[class.cdtor]/3

To form a pointer to (or access the value of) a direct non-static member of an object obj, the construction of obj shall have started and its destruction shall not have completed, otherwise the computation of the pointer value (or accessing the member value) results in undefined behavior.

关于c++ - 在调用析构函数之前对象的生命周期结束了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com