See also Initialization Procedure for Static Variables.
For gcc use init_priority:
http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
It works through different translation units. So your code will read:
Foo* Foo::singleton __attribute__ ((init_priority (2000))) = new Foo();
Bar* Bar::singleton __attribute__ ((init_priority (3000))) = new Bar();
I don’t have gcc right now, so I can’t verify this, but I have used it before. Another simpler and more portable solution is to avoid static initialization and explicitly create singletones in a specific place inside the main one.
void main(void)
{
{
Foo* Foo::singleton = new Foo();
Bar* Bar::singleton = new Bar();
}
...
}
, gnarly , .