Destruction order of static objects in C++

Gal Goldman picture Gal Goldman · Jan 22, 2009 · Viewed 34.7k times · Source

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?

Answer

user3458 picture user3458 · Jan 22, 2009

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.