Determines that an object has been initialized before it is used
Principle: Whether it is a member variable of a class or a variable of another scope, it is guaranteed to be initialized (or assigned) before use.
Initialization of a built-in type without any members
/* The built-in type is initialized by copying */int x = 0;const char* a = "abc"; int a[2] = {0,0};
Second, STL container initialization
STL containers focus on container size, prevent out of bounds, initialize work without concern
Third, class member variable initialization
Reference: http://www.cnblogs.com/BlueTzar/articles/1223169.html
Class Cexample {public: int A; float B; Constructor initialization list cexample (): A (0), B (8.8) {} //constructor internal assignment Cexample () { a=0; b=8.8; }};
The result of the two constructors in the above example is the same. The above constructor (using the constructor of the initialization list) explicitly initializes the members of the class, whereas constructors that do not use the initialization list are assigned values to the members of the class and are not explicitly initialized.
Initialization and assignment do not make much difference to members of built-in types, like any of the above constructors. For non-built-in type member variables, to avoid two constructs, it is recommended to initialize the list with class constructors. But sometimes you have to use a constructor with an initialization list:
1. The member type is a class that has no default constructor. If no display initialization is provided, the compiler implicitly uses the default constructor of the member type, and if the class does not have a default constructor, the compiler will fail with the default constructor.
A member of a 2.CONST member or reference type. Because const objects or reference types can only be initialized, they cannot be assigned a value.
What is the meaning of initializing data members and assigning values to data members? What's the difference?
First, the data members are categorized by type and described:
1. Built-in data type, composite type (pointer, reference)
In the member initialization list and in the constructor body, both performance and results are the same
2. User-defined type (class type)
The results are the same, but there is a big difference in performance. Because a data member object of the class type has been constructed before entering the function body, that is, the work of constructing the object at the member initialization list, calling the constructor, after entering the function body, is the assignment to the already constructed class object, and the copy assignment operator is called to complete (if not provided, Use the default by member assignment behavior provided by the compiler)
Note:
Initialize the member initialization order of the list:
When C + + initializes class members, it is initialized in the order in which they are declared, not in the order in which they appear in the initialization list.
Iv. Duplicate inheritance of C + + inherits the order of initialization of derived class objects
#include <iostream>using namespace Std;class Basea{public:basea () { cout << "Basea class." << Endl ; }};class Baseb{public:baseb () { cout << "Baseb class." << Endl;}}; Class Deriveda:public Baseb, Virtual public Basea{public:deriveda () { cout << "Deriveda class." << Endl; }};class derivedb:public baseb, Virtual public basea{public:derivedb () { cout << "Derivedb class." << E Ndl }};class derived:public Deriveda, Virtual public derivedb{public:derived () { cout << "Derived class." << Endl }};int Main () {Derived obj; cout << Endl;}
Duplicate inheritance (repeated inheritance): A derived class inherits the same base class more than once.
But C + + does not allow a derived class to inherit directly from the same base class two or more times.
Two kinds of duplicate inheritance: Copy inheritance and shared inheritance
Shared inheritance in duplicate inheritance: Enables duplicate base classes to store only one copy in a derived object instance by using a virtual base class.
Initialization order rules for derived class objects that involve sharing inheritance
① first calls the constructor of the virtual base class.
② second calls the constructors of the ordinary base class, and multiple base classes are listed in the order from left to right as they are declared by the derived class.
③ calls the constructor of the object member again, in the order in which the object members appear in the class declaration.
④ finally executes the constructor of the derived class.
Analysis: Class hierarchy relationships of various types are
①derived derived from Deriveda and virtual base class Derivedb
②deriveda derived from Baseb and virtual base class Basea, Derivedb derived from Baseb and virtual base class Basea
Execution order (constructor)
By the ① layer, according to the rules can be ordered as derivedb,deriveda,derived.
Then, for Derivedb, the same order in which the rules are further analyzed is basea,baseb,derivedb.
For Deriveda, it is important to note that both Deriveda and Derivedb are derived from the virtual base class Basea, so based on the processing method that only stores one copy,
Since Basea has already been initialized in Derivedb, Deriveda will not have to be initialized again, so the execution is Baseb, Deriveda.
The last is derived.
The corresponding constructor order can be synthesized: basea (), Baseb (), Derivedb (); Baseb (), Deriveda (); Derived ();
Effective C + + clause 04 (object initialization) collation