C ++ member initialization list

Source: Internet
Author: User

C ++ member initialization list

 

C ++ provides the class member initialization list for the class.

The construction order of class objects is as follows:
1. allocate memory. when calling the constructor, the data members are initialized implicitly/displayed.
2. After Entering the constructor, execute general calculations in the constructor.

There are two reasons for using the initialization list:

1. You must do this:
If we have a class member, which is a class or a structure, and this member has only one constructor with parameters, and there is no default constructor, to initialize this class member, you must call the parameter-containing constructor of this class member. If there is no initialization list, it will not be able to complete the first step and an error will be reported.

 

Class ABC
...{
Public:
ABC (int x, int y, int Z );
PRIVATE:
Int;
Int B;
Int C;
};
Class myclass
...{
Public:
Myclass (): ABC (1, 2, 3 )...{}
PRIVATE:
ABC;
};
 
Because ABC has a displayed constructor with parameters, it cannot rely on the compiler to generate a constructor without parameters. Therefore, without three int-type data, ABC objects cannot be created.
The ABC class object is a member of myclass. to initialize this object ABC, you can only use the Member to initialize the list. There is no other way to pass the parameter to the ABC class constructor.
In another case, when a class member contains a const object or a reference, they must also initialize the object through the member initialization list, because these two objects need to be initialized immediately after Declaration, and in the constructor, they are assigned a value, which is not allowed.

2. The efficiency requirement is as follows:
The construction sequence of class objects is displayed. after entering the constructor body, the computation is performed to assign values to them. Obviously, the assignment and initialization are different, this shows the difference in efficiency. If you do not need to initialize the class table, the class performs an implicit default constructor call for its class members, and a copy operator call, if it is a class object, the efficiency will not be guaranteed.

Note: The data members to be initialized by the constructor will be initialized here, whether or not they are displayed in the constructor's member initialization list, in addition, the initialization sequence is consistent with the declared sequence and has nothing to do with the order of the list. Therefore, you must pay special attention to ensure that the two are consistent in order to truly ensure their efficiency.

For clarity, suppose there is a class:
Class Foo {
PRIVATE:
Int A, B;
};
1. Both Foo () {} and foo (INT I = 0) {} are considered as Default constructors because the latter is the default parameter. The two cannot appear at the same time.
2. The Initialization Method of the constructor list is not in the order of the list, but in the order of variable declaration. For example, in Foo, if A is before B, A is constructed before B. So whether Foo (): A (B + 1), B (2) {} Or Foo (): B (2), a (B + 1) {} won't let a get the expected value. It would be better to declare B and then declare.
3. Const members can be initialized in the const list. For example, if Foo contains an int const C, Foo (int x): C (x) {} can assign the C value to X. Note that C must have a value in each Constructor (if multiple constructor functions exist.
4. in inheritance, only the initialization list can construct private members of the parent class. For example
Class child: Public Foo {
}
The constructor in foo is written as follows: Foo (int x) {A = x ;}.
Writing child (int x) {Foo (x);} in child cannot be compiled. You can only change the initialization of the parent class to Foo (int x): A (x) {}, and write child (int x): Foo (x) {} in the subclass structure.

Another article on the initialization list:

C ++ initialization class members can be completed not only by constructor, but also by initializing the class member list. This method is widely used by MFC. For example, some beginners may not understand the following code:
Class
{
Public:
Int member_var; // member variable
A (); // Constructor
}
A: A (): member_var (0)
{
}

They think that the definition of this constructor can only be written as follows:
A: ()
{
Member_var = 1;
}

Both methods are available. However, in some cases, only the first method can be used, and the first method is usually more efficient.

In fact, the first method is true initialization, while the "=" operation implemented in the constructor is actually a value assignment (assign ). The difference between the two methods starts from here. The differences are as follows:

We know that normal variable compilers will initialize for you by default. They can both be initialized and assigned values, while constants (const) can only be initialized according to their meaning and cannot be assigned values. Otherwise, there will be no difference with the variable. Therefore, constant members (const member) can only use the member initialization list to complete their "initialization", rather than "assign value" to them in the const function ".
We know that class object initialization is actually completed by calling its constructor. If no constructor is written, the compiler will generate one for you by default. If you customize constructors with parameters, the compiler will not generate Default constructors. In this way, the initialization of this class object must have parameters. If such a class object is a member of another class, you must pass a parameter to the constructor of the Class Object to initialize this member. Similarly, if you use "=" in the constructor of the class that contains it, it is actually "assigning value" to this object rather than "initializing" it. Therefore, all constructors in a class have parameters. If such a class is a member variable of other classes, you must initialize it explicitly, you can only complete initialization through the member initialization list. For example:
Class B
{
......
}

Class
{
Public:
B member_ B;
A ();
}
A: A (): B (...) // You must initialize it explicitly because all of its Constructors
// All parameters are included before being assigned a value.
{
B =...; // The value can be assigned only when it is initialized as previously written. Otherwise, the value is incorrect.
}

 

--------------------------------------

Initialization sequence:

Class Test
{

Const int;

STD: String STR;

Object O;

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

{

}

};

 

The yellow one is the initialization list. They will be called before the constructor is officially called, and their initialization order is not initialized according to the order in the initialization list, but the order they declare. As shown above:

The initialization sequence is a, STR, O;

It is generally used to initialize data of the constant type, static type, or data that cannot exist independently.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhoukuo1981/archive/2008/12/02/3430817.aspx

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.