Terms 4:make sure that objects is initialized before they ' re used.
To determine that an object was initialized before it was used.
Initialization of C + +
Let's start with a Java initialization.
class student{ private String Name; Private int Age ; Student () { this. Name = "Destino74"; this. age =; }}
View Code
But in C + +, the form of assigning a member variable is not initialized. The initialization of a member variable of an object occurs before entering the constructor body, so the above only changes its value after the member variable is initialized, and the initialization occurs before those members ' default constructor calls.
In C + +, initialization is the substitution of an assignment with an initialization sequence (member initialization):
#include <string>classstudent{ Public: Student (): M_name ("Destino74"), M_age ( -){} ~Student () {}voidShow () {printf ("name:%s age:%d", M_name.c_str (), m_age); }Private: std::stringM_name; intm_age;};intMain () {Student stu; Stu. Show (); return 0;}
View Code
Although the initialization cost of the built-in opposite (M_age) is the same as the assignment, most types use initialization to be more efficient than the assignment, since using the default constructor constructs a right value and assigns an rvalue to the lvalue with the copy assignment operator. It is often more efficient to call the copy constructor only once.
In general, it is recommended that all members of an object be initialized one at a time in the initialization list, not only for performance reasons, but also for reduced debugging costs: This allows you to not have to worry about which member has been or is still not initialized, and there is no fear of ambiguous behavior resulting from uninitialized use of the object.
In addition, the book mentions to focus on the initialization order. Because there is a definite initialization order in C + +: The base class is always initialized earlier than the derived class, and the member variables of the class are always initialized in a declaration order.
Although you can initialize sequences in which member variable initialization sequences appear in a chaotic order, this does not affect their initialization order.
The last mention is the problem with the initialization sequence of the non-local static variable (global static variable).
When a global static variable is used in a file, how do you ensure that the global static variable has been initialized?
Book:C + + does not have a clear definition of the initialization order for "non-local-static objects defined in different compilation orders." In other words, it is difficult or impossible to determine the order in which they are initialized.
But with a small design you can solve this problem from the Local-static object (local static variable) features: C + + guarantees that the local static object within the function is initialized when the function is called "The first time a definition of the object is encountered." Learning the singleton pattern is clear, maintaining the only instance that is initialized on the first call, followed by the invocation of the object reference, which is lazy initialization of the object (lazy-type). Its merits are no longer elaborated.
Each member variable is initialized with the initialization sequence of the constructor, which is often more efficient than the assignment. The initialization sequence lists the member variables that should be in the same order as they are declared in the class.
To exempt the "initialize sequence across compilation units" issue, replace the non-local static object with a local static object
"Effective C + +" NOTE: II