They are initialized before the program starts (i.e., before main entered).
If there are two or more definitions (static data) in one CPP file, they are initialized in the sequence in which they are defined in the file (the one that was defined earlier / above in the file was initialized until the next).
If there are two or more definitions (static data) in more than one CPP file, the sequence in which the CPP files are processed is undefined / specific-specific. This is a problem if the global variable constructor (called before the program starts) refers to another global variable defined in another CPP file that may not have been created yet. However, paragraph 47 of Meyers' Effective C ++ (which is called "Make sure that global objects are initialized before they are used") describes a description of the workflow ...
Define a static variable in the header file (it is static, so you can have multiple instances of it without a complainer)
Ask the constructor of this variable to call everything you need (in particular, build global singletones declared in the headers)
... which, he said, is a technique that can be used in some system header files, for example. so that the cin global variable is initialized before the static variable constructors use it.
ChrisW Sep 14 '09 at 14:01 2009-09-14 14:01
source share