A constructor initialization list starts with a colon, followed by a comma-separated list of data members, each data member followed by an initializer in parentheses. For example:

class CExample {
public:
    int a;
    float b;
    //构造函数初始化列表
    CExample(): a(0),b(8.8)
    {}
    //构造函数内部赋值
    CExample()
    {
        a=0;
        b=8.8;
    }
};

In the above example, the results of the two constructors are the same. The above constructor (the one using the initialization list) explicitly initializes the members of the class; while the constructor not using the initialization list assigns values to the members of the class, without explicit initialization.

Initialization and assignment make little difference for members of built-in types, so either constructor above works. For member variables of non-built-in types, to avoid constructing twice, it is recommended to use the class constructor initialization list. But sometimes a constructor with an initialization list is required:

  • 1. The member type isa class without a default constructor. If no explicit initializer is provided, the compiler implicitly uses the default constructor of the member type. If the class has no default constructor, the compiler's attempt to use the default constructor will fail.const members
  • 2.const membersorreference typemembers. Because const objects or reference types can only be initialized, not assigned to them.

What does it mean to initialize data members versus assign to data members? What is the difference?

First, classify data members by type and explain each case:

  • 1.Built-in data types, compound types (pointers, references)- Doing it in the member initialization list or in the constructor body is the same in both performance and result.
  • 2.User-defined types (class types)- The results are the same, but there is a big difference in performance. Because class-type data member objects are already constructed before entering the function body, that is, the construction work is done at the member initialization list, calling the constructor. After entering the function body, what is done is assigning values to the already constructed class objects, which requires calling a copy assignment operator to complete (if not provided, the compiler-provided default memberwise assignment behavior is used).

Notes:

Member initialization order in the initialization list:

C++ initializes class members in the order they are declared, not in the order they appear in the initialization list.

class CMyClass {
    CMyClass(int x, int y);
    int m_x;
    int m_y;
};

CMyClass::CMyClass(int x, int y) : m_y(y), m_x(m_y)
{
};

You might think that the above code will first do m_y=I, then m_x=m_y, and finally they have the same value. But the compiler initializes m_x first, then m_y, because they are declared in that order. As a result, m_x will have an unpredictable value. There are two ways to avoid this: one is to always declare members in the order you want them to be initialized; the second is, if you decide to use an initialization list, always list the members in the order they are declared. This will help eliminate confusion.

Original address: http://www.cnblogs.com/BlueTzar/articles/1223169.html