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 (08.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.member types are classes that do not have a 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.Const Member or member of a reference type. Because Constobjects or reference types can only be initialized, and 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 classify data members by type and explain 1. built-in data type, composite type (pointer, reference)
????
2. user-defined type (class type)
????

Note:
Initialize the member initialization order of the list :
???? C + + When initializing class members, they are initialized in the order in which they are declared, 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 be done first m_y=i , and then do m_x=m_y , and finally they have the same value. But the compiler initializes the m_xFirst,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.