You need to use the initialization member list in the following three cases:
1. The data member to be initialized is an object;
2. Class Members whose const needs to be initialized;
3. initialize and reference the member data;
Cause:
C ++ can define member variables of the reference type. member variables of the reference type must be initialized in the initialization list of the constructor. The assignment operation is not allowed if the class member is const modifier or the reference type. (obviously, the const prevents misassignment, the reference type must be defined and assigned together). Therefore, you can only use the initialization list alignment for initialization. The member type is a class without a default constructor. If the display initialization type is not provided, the compiler implicitly uses the default constructor of the member type. If the class does not have the default constructor, the compiler will fail to use the default constructor. (That is, the initialization list must be used in these three cases)
We define a person class as follows:
Class person {
Public:
Person () {}// default constructor Function
Person (string name, string phone, string ADDR)
{
M_name = Name; // you can assign values to initialize data members.
M_phone = phone;
M_addr = ADDR;
}
PRIVATE:
Const string m_name;
Const string m_phone;
Const string m_addr;
};
After compilation, it is found that the second constructor with parameters of this class is incorrect. We create a person object:
Person P ("marcky", "13233232", "cqupt"); // The process of calling a constructor with parameters to create a person object is divided into two steps:
1. Allocate the actual space from the memory to object P. The data members of the three string objects are the default constructor called and the initialization is empty. So far, all three data members of object P are empty strings.
2. Execute the called Constructor's function body statement to assign values to data members so that we can create a specified person object instead of an empty object.
From the second step, we can see that we are assigning values to the three const objects, which is obviously not allowed. Therefore, using this const function to create a person will fail. To successfully create a specific person object, we need to initialize the list of constructors:
Person (string name, string phone, string ADDR)
: M_name (name), m_phone (phone), m_addr (ADDR) {} // The colon (:) begins to define the initialization list. The constructor that uses the initialization list to create an object is also completed through the preceding two steps, the difference is that the default constructor is not used to create data members of an object. Instead, the corresponding constructor is called Based on the specified parameters to create a specific object rather than a null object. In this way, the specific value of the data member of the object is assigned to the corresponding member when the object is created, instead of modifying the data member through the value assignment statement after the object is created, therefore, the const data member can be created successfully using the const initialization list.
There is no default constructor class type member. If it is not initialized in the initialization list, the corresponding "real parameter" is not specified when this object is created ", the compiler will call the default constructor to create an object, which will inevitably end with failure.
Note 1: The initialization sequence of data members is irrelevant to the order in the constructor initialization list, but consistent with the defined sequence of members.
Note 2: It is more efficient to use the initialization list. If a value is assigned to the constructor, it is a copy. If it is an initialization list, it is initialization. Of course, the efficiency of the assignment and Initialization is different.