Topic: Global Object Initialization Order


Author: susukita@kupns1.scphys.kyoto-u.ac.jp (Ryutaro Susukita)
Date: Thu, 15 Dec 1994 15:02:25 GMT
Raw View
In article <3cfv75$g1d@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:

> I have seen the following:
> - in the order of the modules presented to the linker
> - in reverse order of the modules presented to the linker
> - in alphabetical order of object file names
> - in reverse alphabetical order of object file names
> - in alphabetical order of address-section names, viz. interleaved
> All of these just fell out of the way the linker worked; they
> were not anybody's deliberate design decision.

> In other words, it is a poor idea for a program to depend on order
> of initialization of static objects.

> You can use the "nifty-counter" trick, which is somewhat clunky but
> portable, described in the ARM, p19.

> Better, do not depend on initialization of global objects.
> There are usually good alternatives.

Please tell me whether it is true in the case of static members of a class.

// A.h
class A{
 public:
 A(){
  //... access to bbb;
 }
 static int bbb;
};

// A.cc
#include "A.h"
int A::bbb = 100;

// xxx.cc
#include "A.h"

A a;
main(){
}

If 'a' is initialized before 'A::bbb' is, the constructor for 'a' does
not work correctly.

susukita