Topic: static initialization order
Author: jean-claude.bourut@gsi.fr (JClaude Bourut)
Date: Mon, 11 Jul 1994 14:19:34 GMT Raw View
My application contains many modules. In one module I need to initialise static objects before
any other modules try to use them.
For the moment, I have a function that forces initialization and uses an initialization counter:
static int initializationCounter = 0 ;
void doInitialization()
{
if ( initializationCounter++) return ;
// real initialization
}
It works for a set of C++ compiler, but I don't know why.
My questions are:
- Why does it work ?
- Will it always work ?
- What do compilers for DLL ?
Thanks
Author: daniels@biles.com (Brad Daniels)
Date: Mon, 11 Jul 1994 15:57:42 GMT Raw View
In article <jean-claude.bourut.29.000E53EE@gsi.fr>,
JClaude Bourut <jean-claude.bourut@gsi.fr> wrote:
>
>
>My application contains many modules. In one module I need to initialise static objects before
>any other modules try to use them.
>
>For the moment, I have a function that forces initialization and uses an initialization counter:
>
>static int initializationCounter = 0 ;
>void doInitialization()
>{
> if ( initializationCounter++) return ;
>
> // real initialization
>
>}
If that actually works, it's pure chance. You're close to a solution which
will work, though. What you need is a initializer class and a static
variable, like this:
class1.h:
class class1 {
// define a class which needs initialization here.
};
class class1initializer {
static int initializationCounter;
public:
class1initializer();
};
static class1initializer class1dummy;
class1.C:
int class1initializer::initializationCounter = 0 ;
class1initializer::class1initializer()
{
if ( initializationCounter++) return ;
// real initialization
}
// Define class1 members here.
Now, every file which needs class1 will include class1.h, which contains
a definition of a static variable of type class1initializer. About the
only thing currently guaranteed about static initialization is that it
occurs in the order in which nonlocal static objects are defined in a file.
Since the header file contains a definition of a variable, every file which
includes class1.h will contain a definition of a static variable of type
class1initializer *before* any use of class1. This means that the static
variable will be initialized first, which means its ctor will be called
first. The ctor does the initialization for class1 the first time through,
so you're all set.
Note that if the class which uses class1 may in turn be used by other classes,
the #include of class1.h should be in the other class's header file, so
that the proper ordering will be forced on classes which don't use class1
directly, but do use some class which in turn uses class1.
It's a mess, and I don't think there's anybody who really likes things the
way they are...
- Brad
--------------------------------------------------------------------------
+ Brad Daniels | "For all men would be cowards if they durst." +
+ Biles and Associates | - (Some 17th century English guy whose +
+ My views, not B&A's | name I forget) +
--------------------------------------------------------------------------
Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 11 Jul 1994 22:06:03 GMT Raw View
Note that this hack is suggested by the latest WP (see ios::Init) for
initializing cout, cerr and clog, though in extremely opaque text.
Jason