C ++局部变量销毁顺序 [英] C++ local variable destruction order

查看:488
本文介绍了C ++局部变量销毁顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个定义的顺序,其中局部变量在C ++(11)中释放?
为了更简洁:在同一范围内两个局部变量的析构函数的副作用将变得可见。



例如:

  struct X {
〜X(){/ * do something * /}
}

int main(){
X x1;
X x2;
return 0;
}

x1

解决方案 x2 在主返回时首先销毁或是在C ++ 11中未定义的订单? div>

在每个类别的存储类(动态分配的对象除外)中,对象以与构造相反的顺序被破坏。


Is there a defined order in which local variables are deallocated in C++ (11) ? To be more concise: In which order will side effects of the destructors of two local variables in the same scope become visible?

e.g.:

struct X{
  ~X(){/*do something*/}
}

int main(){
   X x1;
   X x2;
   return 0;
}

Is x1 or x2 destroyed first when main returns or is the order undefined in C++11?

解决方案

Within each category of storage classes (except dynamically alloated objects), objects are destructed in the reverse order of construction.

这篇关于C ++局部变量销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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