Since constructor initialization lists and constructor body assignments can assign values to member variables, what is the difference between them?
Constructors can be performed in two phases: (1) initialization phase, (2) General calculation stage. The calculation stage is composed of all the statements in the function body. Regardless of whether the member is explicitly initialized in the constructor initialization list, the data member initialization of the class is always in the initialization phase, preceded by the calculation phase. The constructor initialization list initializes the members of the class, while the constructor body simply assigns a value to the data members of the class.
The constructor initialization list simply specifies the initial value of the member and does not specify an initialization order, so what is the order of member initialization? The initialization order of members is the order in which members are defined, the first member of the definition is initialized first, then the second, and so on.
First, if the data members of the class are static (const) and reference types, you must use the initialization list
Static (const) data members can only be initialized and cannot be assigned, and the same reference type is only initialized, then the list is initialized.
Such as:
C + + code #include <iostream> #include <string> using namespace std; template<class t> class namedptr { public: namedptr (const string& initname, t &NBSP;*INITPTR); private: const string name; // Initialization of static data members must be in the initialization list t * const ptr; }; template<class t> namedptr<t>::namedptr (const string& initname, t *initptr ) : name (Initname), ptr ( INITPTR) {} //The second method is to assign values in the constructor body: //template<class t> //namedptr<t>::namedptr (const string& initname, t *iNITPTR) //{ // name = initname; // ptr = initptr; //} int main () { int a = 10; namedptr<int > test ("Shenzhen", &a); }
Because of the const string name; Initialization of static data members must be in the initialization list
T * const PTR;
is static, and if the constructor body is assigned a value, the compilation can be faulted.
Second, the constructor body assignment will incur additional overhead, less efficient than the constructor initialization list
The above example changes:
C + + code Template<class t> class Namedptr {public:namedptr (const string& initname, T *initptr); Private:string name; Initialization of static data members is required with the initialization list T * PTR; };
and use these two initialization methods to do the comparison:
C + + code//First method: Initialize list Template<class t> namedptr<t>::namedptr (const string& initname, T *initptr) : Name (Initname), PTR (initptr) {}//The second method is to assign values in the constructor body: Template<class t> namedptr<t>::namedptr (con St string& Initname, T *initptr) {name = Initname; ptr = initptr; }
When a data member is initialized with the second method, it is called two times for a member function of string: One is the default constructor and the other is an assignment.
The first method (the initialization list) simply calls the default constructor and does not call the assignment function. Reduces unnecessary spending, and when the class is fairly complex, you'll see the benefits of using an initialization list.