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++中静态对象的销毁顺序


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16