在C ++中静态对象的破坏顺序 [英] Destruction order of static objects in C++

查看:189
本文介绍了在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 ++中静态对象的破坏顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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