Article From:https://www.cnblogs.com/yaoyudadudu/p/9216870.html

There have been doubts about two different initialization of C++ class constructors, and even do not know what to call.

Original text: https://www.cnblogs.com/BlueTzar/articles/1223169.html

———————–Text segmentation line – — – — – —-

The constructor initialization list begins with a colon, followed by a comma separated list of data members, each of which is followed by an initializer in parentheses.。For example:

class CExample {
public:
    int a;
    float b;
    //constructor initializer list
    CExample(): a(0),b(8.8)
    {}
    //Internal assignment of constructor
    CExample()
    {
        a=0;
        b=8.8;
    }
};

 

In the above example, the result of the two constructors is the same.AboveConstructor (using constructor of initialization list) explicitly initializes class members.;andThe constructor that does not use the initialization list is assigned to members of the class without explicit initialization.

Initialization and assignmentThere is no big difference in the members of built-in type (such as int, float, etc.), like any constructor on the above.For non built-in type member variables, in order to avoid two construction,Recommended useClass constructor initialization list.But sometimes you must use constructors with initialization lists:
1.A member type is a class that does not have a default constructor.If the display initialization is not provided, the compiler implicitly uses the default constructor of the member type, and if the class does not have the default constructor, the compiler attempts to use the default constructor to fail.
2.
constmemberA member of a type of reference.。Because const objects or reference types can only be initialized, they can not be assigned. It

What is the meaning of initializing data members and assigning values to data members? What’s the difference?
First, classify data members according to their types and explain them in detail.
1.Built in data type, composite type (pointer, reference)
    In the member initialization list and the constructor, it is the same in performance and result.
2.User defined type (class type)
    
The result is the same, but there are great differences in performance.Because class type data member objects have been constructed before entering the function body, that is to say, the work of constructing objects at the member initialization list is called the constructor, after entering the function body, the assignment of the already constructed class objects is performed, and a copy assignment operator can be transferred to complete (such asIf the result is not provided, the default member assignment is provided by the compiler.

Note:
The initialization order of the initialization list:
    C++When initializing class members, they are initialized in the order of declaration, rather than in the order in which they appear in the initialization list.
    Example:

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 may think that the above code will first do m_y=I, then do 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 such a sequence. The result is that m_x will have an unpredictable value.There are two ways to avoid it. One is always declaring members in the order that you want them to be initialized, and the second is that if you decide to use the initialization list, you always list the members in the order they declare. This will help to eliminate confusion.

Leave a Reply

Your email address will not be published. Required fields are marked *