One, in the following cases, you must use the member initialization list:
1, when the initialization of a reference;
2, when the initialization of a const member;
3, when calling a base class constructor, and it has a set of parameters;
4, when calling a member class constructor, and it has a set of parameters.
In these four cases, the initialization should be in the initialization list, otherwise the efficiency is not high.
Second, another common question is: can I call a member function to set an initial value of a member?
The answer is yes, and here's a code conversion:
X::X (int val) : I (Xfoo (Val)), J (Val) {}// is actually converted as follows x::x (/* */ int val) { this,Xfoo (val); = val;}
In this conversion, the this pointer has been constructed properly. However, it is best to put the function in the constructor body, because the members it uses may not be initialized yet.
Explore the C + + object model in depth->2.4 members ' initialization team