Class filesystem {...}; // This class is in filesystem thefilesystem in your // library; // Library User // interaction with this object ////////////////////////////// /// // class directory is created by the user in the Library: directory ();...}; directory: Directory () {create a directory object by calling thefilesystem's member functions ;}
Directory tempdir; // temporary file directory
Now, the initialization order becomes obvious: Unless thefilesystem is initialized before tempdir, The tempdir constructor will use the uninitialized thefilesystem. However, thefilesystem and tempdir are created by different people at different times and in different files. How can I confirm that thefilesystem was created before tempdir?
In a specific compiled unit (that is, the source file), the constructor can ensure that the object is initialized during creation. However, if the object is in a compiled unit, the initialization of an object depends on the value of another object in another compiled unit, and the second object itself needs to be initialized, which makes things more complex.
Non-local static objects refer to the following objects:
· Defined in global or namespace
· Declared as static in a class, or
· A file is defined as static.
For non-local static objects in different compiled units, you do not want your program behavior to depend on their initialization sequence because you cannot control this sequence. That is, you cannot control the initialization sequence of non-local static objects in different compiled units.
Solution: first, transfer each non-local static object to the function and declare it as static. Next, let the function return the reference of this object. In this way, the user will specify the object through function calls. In other words, the static object inside the function replaces the non-local static object.
Although C ++ has hardly explained when the "non-local" static object is initialized, the static object in the function (that is, "local" static object) when the object is initialized, C ++ makes it clear that they are initialized when the object definition is first encountered during the function call process. Therefore, if you do not directly access non-local static objects, instead, use function calls that return local static object references, the reference obtained from the function can point to the initialized object. Another benefit of doing so is that if the function that simulates non-local static objects has never been called, it will never overhead object construction and destruction; this is not a good thing for non-local static objects.
Class filesystem {...}; // same as the previous filesystem & thefilesystem () // This function replaces the {// thefilesystem object static filesystem TFs; // define and initialize // local static object // (TFS = "the file system") return TFs; // return its reference} class directory {...}; // same as the previous directory: Directory () {, except that thefilesystem is replaced by thefilesystem ();} directory & tempdir () // This function replaces {// tempdir object static directory TD; // defines and initializes // partial static object return TD; // returns its reference}
There are two global objects. We know that they will be initialized in order when put in the same CPP (the compiler generates a constructor call in order). If these two global objects are in different CPP, is there a way to control the calling order of the constructor? (Link issue)
The initialization sequence of global objects is not defined in the C ++ standard.
You can do this:
Suppose you need a class A Object A, so write
A & geta ()
{
Static;
Return;
};
This ensures that A is initialized when a is used.
Refer:
Http://bbs.csdn.net/topics/30293003
Article 47: Ensure that non-local static objects are initialized before use