The default constructor for merging is defined as Delete.

Source: Internet
Author: User
1. Default Initialization

If no initial value is specified during variable definition, the variable is initialized by default, and the variable is assigned the "default value ".

For class variables, initialization relies on constructors. Therefore, even if Initialization is not provided when a class variable (object) is defined, we can complete initialization through the default constructor of the class.That is, the "default value" is determined by the default constructor (provided that the class has a default constructor ).

For built-in variables, when defining variables without initialization, the system sometimes initializes the variables. The "default value" is determined by the defined position: the variable defined outside the function body is initialized to 0; the default value of the variable defined inside the function body is undefined and will not be automatically initialized by the system, although the value is undefined, there is still a default value.That is, the "default value" is determined by the defined position.

2. default values for the const type

Once a const object is created, its value cannot be changed. Therefore, the const object must be initialized (note that the user must decide how to initialize the object to specify the default value, rather than relying on the system ).

For built-in type variables, there is only one way to provide values during initialization. That is, the const built-in type variables cannot be initialized by default, and the system does not provide the default value. Therefore, the following code syntax is incorrect:

Const int ival; // error, ival is not initialized

For class type variables, we use constructors to initialize the variables, which also includes the default constructor. Therefore, the initialization type is not provided when defining objects:

Const a object; // correct, initialized by default constructor
3. The default constructor for merging is defined as Delete.

1. The default constructor created by the compiler initializes data members of the class according to the following rules:

-> If the data member has an inclass initial value, the inclass initial value is used in the default constructor to initialize the member.

-> If the data member does not have an internal class initial value, the Member is initialized by default in the default constructor. (To be more precise, use the default value of this Member)

2. Contact primer 5th page450-451

-> If there is a class without an in-class initializerConst built-in type memberThe member cannot be initialized (because the const built-in type variables must be initialized by the user, note: the const built-in type variables cannot be initialized by default ). Because this member cannot be initialized by itself (through the two methods above), the default constructor of this class is defined as Delete ).

-> If there is a class without an in-class initializerConst class membersAnd its type does not define the default constructor (as a result, this const class type member cannot be initialized by default), so the default constructor of this class is defined as Delete ).

-> If there is a class type member in the class that does not have an inner class initializer, and its type does not define the default constructor (as a result, the class type member cannot be initialized by default ), therefore, the default constructor of this class is defined as Delete ).

Summary of the delete statement:

1. First, make it clearBuilt-in type variablesWhether the initialization can be performed by default depends on the variable location. However, for non-const built-in type variables, a default value is always available, even if it may be undefined.Class Type VariableTo have a default value, you must have a default constructor. If there is no default constructor, it cannot be initialized by default.

2. For built-in type members without class inner initiators, const makes the members lose the ability to have default values.

3. for class type members without class inspector, adding or not const is the same, because it has the default value ability, it is determined by whether the member has the default constructor for default initialization.

The default constructor for merging is defined as Delete.

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.