Performance issues, it is necessary to initialize the list in some situations where the initialization list
- Constant members, because constants can only be initialized and cannot be assigned, so they must be placed in the initialization list
Error1 (ConstChar* Constmsg):d ATA (msg)
{
data = msg;
}
- Reference type, the reference must be initialized at the time of definition and cannot be re-assigned, so it is also written in the initialization list
- There is no default constructor for the class type, because using an initialization list can be initialized without calling the default constructor, but instead calling the copy constructor directly.
class Error2
{
char * data;
Public :
Error2 (ConstChar* Constmsg= 0):d ata (msg){}//error2 ()
};
class Error1
{
char * data;
Error2 E2;
Public :
Error1 (ConstChar* Constmsg=0):d ata (msg)
{
e2 = E2out;
data = msg;
}
Error1 (const char* Const MSG = 0):d ata (msg) {}
Error1 (ConstChar* Constmsg, Error2&E2out):d ATA (msg), E2 (E2out)
{
e2 = E2out;
data = msg;
}
};
If not, there'll be a waring,
-
-
Using the same calling code, the output is as follows.
The first line of output corresponds to the first line of the calling code. The second line outputs the initialization list of the corresponding Test2, invoking the copy constructor directly to initialize the Test1, eliminating the process of invoking the default constructor. So a good rule is to use the initialization list whenever possible using the initialization list.
The Initialize list of C + + Class