Destruction of object in local scope

为君一笑 提交于 2019-12-11 12:46:15

问题


Suppose that I have the following code:

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

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)?


回答1:


Yes, it is guaranteed:

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



来源:https://stackoverflow.com/questions/20927795/destruction-of-object-in-local-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!