C + + Basic syntax: constructor initialization list _c language

Source: Internet
Author: User

C + + provides an initialization list of class members in a class

Class objects are constructed in the order that they are:
1. Allocate memory, call the constructor, implicit/display initialization of each data member
2. Perform a general calculation in the constructor after entering the constructor function

There are two reasons to use the initialization list:

1. This must be done:
If we have a class member, it itself is a class or a struct, and this member has only one constructor with parameters, and there is no default constructor, then to initialize the class member, you must call the constructor with parameters of the class member, if there is no initialization list, Then he will not be able to complete the first step, will be the error.

Copy Code code as follows:

Class ABC
... {
Public:
ABC (int x, int y, int z);
Private:
int A;
int b;
int C;
} ;
Class MyClass
... {
Public:
MyClass (): ABC (1, 2, 3) ... {}
Private:
ABC abc;
} ;

Because ABC has a constructor that is displayed with parameters, then he cannot rely on the compiler to generate an parameterless constructor, so the ABC object cannot be created without three int data.

The ABC class object is a member of the MyClass, and to initialize the object ABC, you can only initialize the list with members, and there is no other way to pass parameters to the ABC class constructor.

Another scenario is this: when a const object is contained in a class member, or as a reference, they also have to be initialized through the member initialization list, since both objects are initialized immediately after the declaration, and in the constructor, they are assigned to them, which is not allowed.

2. Efficiency requires that:
The construction order of the class object shows that after entering the constructor body, the calculation is performed, is the assignment to them, obviously, the assignment and initialization are different, so that the efficiency difference, if not the member initialization Class table, then the class to its own class members are an implicit default constructor calls, And a copy of the operator's call, if it is a class object, this efficiency is not guaranteed.

Note: constructors need to initialize the data members, regardless of whether the display appears in the constructor's member initialization list, the initialization is done at that point, and the order of initialization is consistent with the order in which it was declared, regardless of the order of the list, so pay special attention to Ensure the consistency of the two in order to truly ensure its efficiency.

To illustrate this, suppose there is a class:

Copy Code code as follows:

Class foo{
Private:
int A, B;
};

1, foo () {} and foo (int i = 0) {} are considered default constructors because the latter is the default parameter. Both cannot appear at the same time.

2. The constructor list is initialized not in the order of the list, but in the order in which the variables are declared. For example, in Foo, a before B, then construct a and then construct B. So whether Foo (): A (b + 1), B (2) {} or foo (): B (2), a (b+1) {} will not let a have the desired value. It would be better if you declared B before declaring a.

3, the constructor list can initialize the const member. Like Foo has an int const C; then foo (int x): C (x) {} can have a C value assigned to X. It is important to note, however, that C must have a value in each constructor (if there are multiple).

4, in inheritance, only the initialization list can construct private members of the parent class. For example

Copy Code code as follows:

Class Child:public foo{
}

The constructor inside Foo is written like this: foo (int x) {a = x;}.
And in the child write child (int x) {foo (x);} is not through the compilation. Only the parent class is initialized to foo (int x): A (x) {} and the subclass constructs the writing child (int x): foo (x) {}.

The members of the C + + initialization class can be completed with constructors (constructor) and by initializing the class member list. MFC uses this method extensively. For example, some beginners may not understand the following code:
Class A
{
Public
int Member_var; Member variable
A (); Constructors
}
A::a (): Member_var (0)
{
}

They felt that the definition of this constructor should be written only as follows:
A::a ()
{
Member_var=1;
}
In fact, both methods are available. But in some cases, only the first one, and usually the first one will be more efficient.

In fact, the first method is true initialization (initialization), while the "=" operation implemented within the constructor is actually an assignment (assign). All the differences between the two methods begin here. The difference is probably as follows:

1. We know that the generic variable compiler will initialize the default for you. They can be initialized as well as assignable, and constants (const) can only be initialized and cannot be assigned in accordance with their meaning. Otherwise, there is no difference from the variable. Therefore, constant members (const member) can only complete their initialization with the member initialization list, and they cannot be "assigned" to them within the constructor.

2. We know that the initialization of the object of the class is actually to call his constructor complete, and if there is no write constructor, the compiler will generate one for you by default. If you customize a constructor with parameters, the compiler will not generate a default constructor. Thus, the initialization of the object of this class must have parameters. If the object of such a class is to be a member of another class, then in order to initialize the member, you must pass an argument for the constructor of the object of the class. Similarly, if you use "=" in the constructor of the class that contains it, it is actually "assigned" rather than "initialized" for the object. so all constructors in a class have parameters, so if such a class is a member variable of another class, you have to initialize it explicitly, and you can only initialize it through the member initialization list. For example:

Copy Code code as follows:

Class B
{
......
}

Class A
{
Public
B Member_b;
A ();
}
A::a (): B (...)//You must explicitly initialize it because all of his constructors
All have parameters, before they can be assigned a value.
{
B= ... //Because it has already been initialized to be assigned as written above, otherwise it will be wrong.
}


Order of initialization:
Copy Code code as follows:

Class Test

{

const int A;

Std:string str;

Object o;

Test (): STR ("DF"), O (null), a (0)

{

}

};


The yellow is both an initialization list, they will be called before the constructor is formally called, and their initialization order is not based on the order in which they appear in the initialization list, but in the order in which they are declared. Above

Initialization order is: A, str, o;

Typically used to initialize constant types, statically typed data, or data that cannot exist independently

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.