In the following four cases, to get the program to compile smoothly, you must use the member initialization list:
1, initialize one of the reference members (reference member);
2, initialize a constant object (const member);
3, the constructor of a base class is called, and the constructor of the base class has a set of parameters;
4, call a constructor of the member class, which has a set of parameters
These four kinds of cases can be compiled normally, but the efficiency is deficient (specifically mentioned below).
Class word{
String _name;
int _cnt;
Public:
Word () {
_name = 0;
_cnt = 0;
}
};
The implementation mechanism of the above program is that the constructor of the word class will be a temporary object of a string class (note that _name is an object of the string Class), and then initialize the temporary object.
The temporary object is then assigned to the _name by the assignment operator, and the temporary object is finally destructor.
The following is the internal expansion result of the constructor, C + + pseudo code:
Word::word ()
{
_name. String::string (); Call the default constructor of the String class (constructor)
String temp = string (0);//Generate a temporary object for the class and initialize the
_name. String::operator = (temp); The value (depth) of the temporary object is copied to _name temp by the assignment operator
. String::~string (); Call the destructor of string
_cnt = 0;
}
The above code is inefficient because the middle requires invoking the default constructor and destructor to generate and destroy a temporary object, which is a more efficient implementation:
Word::word: _name (0) //_name directly calls the constructor of the string class to assign a value to
{
_cnt = 0;
}
It will be expanded by constructors into the following form (c + + pseudo code)
Word::word ()
{
_name. Sting::string (0); Call string (int) constructor
_cnt = 0;
}
The member initialization list is not a set of function calls, compiler one by one operations initializes the list, inserts the initialization action in the constructor in the appropriate order, and is performed before the programmer explicitly writes the code.
The order of items in the list is determined by the order of the member declarations in the class, not by the order in which the initialization list is arranged. The confusion of "initialization order" and "order of items in the initialization list" can cause unexpected errors:
Class X {
int i;
Int J;
Public:
X (int value): j (value), I (j)
{} ....
The author of the above code is meant to set the initial value of J to value, and then set the initial value of I to J. However, because the declaration order I is preceded by J, the I (j) in the initialization list actually executes earlier than J (value),
This brings about an unexpected error. The correct wording should be:
Class X {
int i;
Int J;
Public:
X (int value): j (value) //J (value) calls the constructor to assign the initial value
{i = j;}
};
Although this writing is still the I declaration before J, however, the error does not occur because the items in the initialization list are inserted into the constructor and are not persisted in the original declaration order, that is, the initialization list is inserted into the constructor to initialize the list in order of precedence over the order in which the code writer explicitly declares it.
This article on the C + + class member initialization list of the related issues are small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.