C ++ class constructor initialization List)

Source: Internet
Author: User

The constructor initialization list starts with a colon, followed by a list of data members separated by commas (,). Each data member is followed by an initialization formula in parentheses. For example:

Class Cexample {
Public :
Int A;
Float B;
// Constructor initialization list
Cexample (): ( 0 ), B ( 8.8 )
// Internal assignment of constructor values
Cexample ()
} ;

In the above example, the results of the two constructors are the same.The above Constructor (using the constructor in the initialization list) explicitlyInitializationClass member. The constructor that does not use the initialization list is a member of the class.AssignmentIs not explicitly initialized.

initialization and assignment have no major difference for built-in type members, just like any of the constructors above. for non-built-in member variables, we recommend that you use class constructor initialization list. but sometimes you must use a constructor with an initialization list:
1. the member type is class without default constructor . If the display initialization type is not provided, the compiler implicitly uses the default constructor of the member type. If the class does not have the default constructor, the compiler will fail to use the default constructor.
2. const member or a member of the reference type . Because the const object or reference type can only be initialized, they cannot be assigned a value.

InitializationData members and data membersAssignmentWhat does it mean? What is the difference?
First, the data members are classified by type and described as follows:
1. built-in data type, composite type (pointer, reference)
In the member initialization list and constructor body, the performance and results are the same.
2. User-Defined type (class type)
The results are the same, but the performance varies greatly. Because the data member objects of the class type have been constructed before they enter the function body, that is, the object construction work is performed at the member initialization list, the constructor is called, and after entering the function body, assign values to the constructed class objects and call a copy assignment operator (if not provided, use the default assignment by members provided by the compiler)

The initialization sequence of the members in the initialization list:
C ++ initializes class members in the declared order instead of in the initialization list.

Class Cmyclass {
Cmyclass (IntX,IntY );
} ;

Cmyclass: cmyclass ( Int X, Int Y): m_y (Y), m_x (m_y)

You might think the aboveCodeM_y = I will be done first, m_x = m_y will be done, and finally they will have the same value. But the compiler first initializes m_x and then m_y, because they are declared in this order. The result is m_x, which will have an unpredictable value. There are two ways to avoid it. One is to declare members in the order you want them to be initialized, and the other is, if you decide to use the initialization list, these members are always listed in the order they are declared. This will help eliminate confusion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.