Article 4: Determine that an object has been initialized before it is used
One
Variables may or may not be initialized under different circumstances.
Note the difference between initialization and assignment.
1. Implicit initialization does not occur in a built-in type in a class, and the ability to customize a default constructor is initialized by default.
Therefore, it is important to initialize the built-in type when constructing the class, and it is best to give the custom object display initialization to avoid wasting resources in the function body.
2. The built-in type is not initialized in the function body and is automatically initialized to 0 outside of the function body.
Two
1. Const and reference types must be initialized and cannot be assigned
Three
1, when the class is really a lot of constructors, and always want to repeat the initialization of some member data, you can consider those "assignment and initialization as good" data assignment into a often private tool function.
Four
1, C + + has a strict initialization order, the member initialization sequence and the order of declaration in the class is a good habit, otherwise there may be obscure errors
Five
The static object within the C + + Guarantee function is initialized the first time it is called, and the order of initialization is guaranteed by replacing a No_local-static object in another file.
1, in a multi-compilation file, the initialization of a class A object in a file depends on the data of a Class B object in another file, the initialization order is an issue because the compiler cannot determine which object of the class in the file can be initialized first. However, the initialization of a class A object depends on the tree flower of another Class B object.
Solution:
A instantiated object in Class A that is prepared for Class B definition, and extern a x changes into an inline with a return & function that defines a static a x, which initializes class B by this function, which guarantees the order in which the object is initialized between classes.
Effective C + + study notes clause 4-7