C + + Learning notes-constructors

Source: Internet
Author: User

Constructor definitions: Each class defines how its objects are initialized, and the class controls the initialization of its objects by one or several special member functions called constructors.

A few things to note:

1: Constructors cannot be declared as const, and when we create a const object, the object can actually get its "constant" property until the constructor finishes the initialization process. Therefore, the constructor can write values to the Const object during its construction.

2: The compiler automatically generates a default constructor only if no constructors are declared in the class. (By: If a class sub-case requires control of the initialization of the object, then the class is likely to need control in all cases--excerpt from C++primer)

3: If a class contains members with built-in types or composite types, this class is appropriate for using the default constructor of a composition only if the members are all given the initial values within the class.

4: If a class contains a member of a type of other class and the type of the member does not have a default constructor, the compiler cannot initialize the member. For such a class we must customize the default constructor, otherwise the class will not have a default constructor available.

5:=default meaning, in the c++11 new standard, if we need the default behavior, then you can ask the compiler to generate the constructor by writing the =default in the argument list. The outside of the class can also appear if =default can appear inside the class. As with other functions, if =default appears inside the class, the default constructor is inline, and if it is outside the class, the member is not inline by default.

6: Member variables in a class are initialized by default before the constructor is executed, so it is more efficient to initialize member variables in the initialization list than to initialize member variables within the constructor.

7: The initialization order of the members is independent of the order in the initialization list, and is related to the order in which they are defined in the class. So be aware of the use of the initialization list to prevent it from initializing another variable when one is not initialized. This is the result of unpredictable errors.

8: If a constructor provides default arguments for all parameters, it actually defines the default constructor.

9: Delegate constructor. A delegate constructor performs its own initialization process with other constructors of the class it belongs to, or it delegates some of its own responsibilities to other constructors.

C + + Learning notes-constructors

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.