销毁本地作用域中的对象 [英] Destruction of object in local scope

查看:133
本文介绍了销毁本地作用域中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

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

〜myclass() 保证在退出本地作用域时被称为 ,或者可以稍后调用(例如函数返回时)?

Is ~myclass() guaranteed to be called by the time the local scope is exited, or might it be called at a later time (such as when the function returns)?

推荐答案

是的,保证:


[class.dtor] 对于具有自动存储持续时间的构造对象,当创建对象的块退出时,会隐式调用析构函数。

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

这篇关于销毁本地作用域中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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