Destruction order of static objects in C++(C++中静态对象的销毁顺序)
问题描述
我可以控制破坏静态对象的顺序吗?有什么办法可以强制执行我想要的命令吗?例如,以某种方式指定我希望某个对象最后销毁,或者至少在另一个静态对象之后销毁?
Can I control the order static objects are being destructed? Is there any way to enforce my desired order? For example to specify in some way that I would like a certain object to be destroyed last, or at least after another static object?
推荐答案
静态对象以与构造相反的顺序销毁.而且施工顺序很难控制.唯一可以确定的是,在同一个编译单元中定义的两个对象将按照定义的顺序构造.其他任何东西都或多或少是随机的.
The static objects are destructed in the reverse order of construction. And the order of construction is very hard to control. The only thing you can be sure of is that two objects defined in the same compilation unit will be constructed in the order of definition. Anything else is more or less random.
这篇关于C++中静态对象的销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++中静态对象的销毁顺序
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
