C + + class constructor initialization list

Source: Internet
Author: User

The constructor initialization list begins with a colon, followed by a comma-delimited list of data members followed by an initialization in parentheses after each data member. For example:

Class Cexample{
Public
int A;
float B;
Constructor initialization list
Cexample (): A (0), B (8.8)
{}
Constructor internal assignment
Cexample ()
{
a=0;
b=8.8;
}
};

The result of the two constructors in the above example is the same. the above constructor (using the constructor of the initialization list) explicitly initializes the members of the class, whereas constructors that do not use the initialization list are assigned values to the members of the class and are not explicitly initialized.

initialization and assignment do not make much difference to members of built-in types, like any of the above constructors. for non-built-in type member variables, to avoid two constructs, it is recommended to initialize the list with class constructors. But sometimes you have to use a constructor with an initialization list:
1. The member type is a class that has no default constructor. If no display initialization is provided, the compiler implicitly uses the default constructor of the member type, and if the class does not have a default constructor, the compiler will fail with the default constructor.
2.a const member or a member of a reference type. Because const objects or reference types can only be initialized, they cannot be assigned a value.

What is the meaning of initializing data members and assigning values to data members? What's the difference?
First, the data members are categorized by type and described:
1. Built-in data type, composite type (pointer, reference)
In the member initialization list and in the constructor body, both performance and results are the same
2. User-defined type (class type)
The results are the same, but there is a big difference in performance. Because a data member object of the class type has been constructed before entering the function body, that is, the work of constructing the object at the member initialization list, calling the constructor, after entering the function body, is the assignment to the already constructed class object, and the copy assignment operator is called to complete (if not provided, Use the default by member assignment behavior provided by the compiler)

Note:
Initialize the member initialization order of the list:
When C + + initializes class members, it is initialized in the order in which they are declared, not 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 might think that the above code will do m_y=i first, then do m_x=m_y, and finally they have the same value. But the compiler initializes the m_x first, then m_y, because they are declared in this order. The result is that the m_x will have an unpredictable value. There are two ways to avoid it, one is to always declare members in the order that you want them to be initialized, and the second is to always list those members in the order in which they are declared, if you decide to use the initialization lists. This will help eliminate confusion.

C + + class constructor initialization list

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.