Class member initialization can be implemented in constructors or in the initialization list of constructors. But the master is generally choose the latter, what is the reason.
Implementation of the constructor
CMyObject:: cmyobject (int ID, string name)
{
m_id = ID;
M_name = name;
Implementation
Cmyobject::cmyobject (int ID, string name) in the initialization list
: m_id (ID), M_name (name)
{
}
Advantages: Eliminates the presence of temporary objects
Method one is to do assignment in the constructor, and method two is to do the pure initialization operation. As we all know, the assignment operation of C + + will produce a temporary object. The presence of temporary objects reduces the efficiency of the program.
Other scenes:
1. A const member exists in the class, and the member must be initialized in the initialization list.
2. If the class contains other Class B as a member, and Class B prohibits the assignment operation, then the object to B can only be implemented by initializing the list.