20. The use of initialization lists

When there is a const variable in the class, the constructor cannot assign a value to it. C++ provides an initialization list to initialize member variables

Test () : ci (10)

{

}

Note: The initialization order of members does not depend on their order in the initialization list, but is the same as the order in which the members are declared.

                 The initialization list is executed before the function body of the constructor

The const members in the class will be allocated space (the allocated space is consistent with the allocated space of the entire class object)

The nature of const members in a class is a read-only variable

A const member in a class can only have an initial value specified in the initializer list

The compiler cannot directly get the initial value of the const member, so it cannot enter the symbol table to become a real constant.

Initialization is different from assignment:

    Initialization: Initialize the object being created.

    Assignment: Set a value to an existing object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885313&siteId=291194637