TITLE: Initialization order of globals PROBLEM: mat@dutncp8 (Mathijn Elhorst), 23 Mar 95 class B { ... }; class A { static B b; public: A() { /* uses member 'b' */ } }; [ This will likely cause a run-time error (bus or seg fault) on this next line! -adc ] A a1; main() { ... }; B A::b; RESPONSE: clamage@Eng.Sun.COM (Steve Clamage) The problem here is that a1 is constructed before A::b, since within one compilation unit objects are constructed in the order in which they are defined (not necessarily declaration order). The original post goes on to discuss the difficulty of controlling the order, since in general A::b would be defined in a different file anyway. C++ provides no good portable way to control the order of initialization of file-scope objects in different files. It's better not to write code that depends on the order of initialization of file-scope objects in different files. In cases where you must, you can use the "nifty counter" technique on page 19 of the ARM (the scheme used by many implementations of iostreams to initialize cin and cout). Or you can access global objects via a function call. Example: Instead of putting T foo(); in a file, hoping it gets initialized at the right time, do this: T& foo() { static T foo_object(); return foo_object; } Then instead of referring to 'foo', you refer to 'foo()'. The static foo_object must be initialized the first time foo() is called, so this eliminates the ordering problem. Now you only need to worry about circular dependencies :-)