Static objects are destroyed in the reverse order in which they are constructed (for example, the first constructed object is destroyed last), and you can control the sequence in which static objects are created using the method described in Section 47, “ Make sure that global objects are initialized before they are use "in Meyers' book Effective C ++."
For example, to somehow indicate that I would like some object to be destroyed last, or at least after another static onject?
Make sure it is created in front of another static object.
How can I control the construction order? not all statics are in the same dll.
I will ignore (for simplicity) the fact that they are not in the same DLL.
My paraphrase from paragraph 47 of Meyers (4 pages long) is as follows. Assuming you are globally defined in a header file like this ...
... add code that includes such a file ...
The effect of this will be that any file that includes GlobalA.h (for example, your original GlobalB.cpp file that defines your second global variable) will define a static instance of the InitA class that will be created before everything else in that source file (for example, before your second global variable).
This InitA class has a static reference count. When the first InitA instance is constructed, which is now guaranteed to be created before the GlobalB instance is created, the InitA constructor can do whatever it needs to do to ensure that the globalA instance is initialized.
ChrisW Jan 22 '09 at 15:46 2009-01-22 15:46
source share