gpt4 book ai didi

c++ - 在局部范围内销毁对象

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:35 25 4
gpt4 key购买 nike

假设我有以下代码:

void foo() {  
{
myclass object;
object.do_something();
}
cout<<"hello"<<endl;
}

~myclass() 保证 在本地作用域退出时调用,还是稍后调用时间(例如函数返回的时间)?

最佳答案

是的,这是有保证的:

[class.dtor] Destructors are invoked implicitly for ... a constructed object with automatic storage duration when the block in which the object is created exits.

关于c++ - 在局部范围内销毁对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927795/

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