gpt4 book ai didi

c++ - 是否保证在 block 结束之前不会调用 C++ 析构函数?

转载 作者:IT老高 更新时间:2023-10-28 12:36:31 27 4
gpt4 key购买 nike

在下面的 C++ 代码中,我是否保证 ~obj() 析构函数将在 //More 代码执行之后被调用?或者如果编译器检测到它没有被使用,是否允许编译器提前销毁它?

{
SomeObject obj;
... // More code
}

我想使用这种技术来避免我不得不记住在 block 末尾重置一个标志,但我需要在整个 block 中保持设置标志。

最佳答案

您可以接受 - 这是 C++ 编程中非常常用的模式。来自 C++ 标准第 12.4/10 节,指的是何时调用析构函数:

for a constructed object with automatic storage duration when the block in which the object is created exits

关于c++ - 是否保证在 block 结束之前不会调用 C++ 析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2087600/

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