C ++ member initialization list

Source: Internet
Author: User

The construction order of class objects is as follows:
1. allocate memory. when calling the constructor, the data members are initialized implicitly/displayed.
The initialization phase can be explicit or implicit, depending on whether a member initialization table exists. In the implicit initialization phase, the default constructor of all base classes is called sequentially in the declared order, and then the default constructor of all member class objects.
2. After Entering the constructor, execute general calculations in the constructor.

The computing phase consists of all the statements in the constructor. In the computing phase, data member settings are considered to be value assignment rather than initialization.

There are two reasons for using the initialization list:

1. You must do this:

Initialize the member list in three cases.
1) object member;
2) const modified member;
3) Reference member data;

(1) If a class member is a class or a structure, and the member has only one constructor with parameters, 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.

Using namespace STD; <br/> Class ABC <br/>{< br/> Public: <br/> ABC (int x, int y, int Z ): A (x), B (Y), C (z) {}; <br/> PRIVATE: <br/> int A; <br/> int B; <br/> int C; <br/>}; <br/> class myclass <br/>{< br/> Public: <br/> myclass (int, int B, int C): ABC (a, B, c) {}< br/> PRIVATE: <br/> ABC; <br/> }; </P> <p> int main () <br/>{< br/> myclass O (1, 2, 3); <br/> return 0; <br/>}< br/>

(2) When a class member contains a const Member

(3) When a class member contains a reference

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 list of members, the class performs an implicit default constructor call for its own 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, no matter whether they are explicitly 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.

Now you understand why you need to use the member initialization list.

The initialization sequence of the class should be emphasized here. The initialization of the class member variables is not initialized in the order of the initialization table, but initialized in the order declared in the class.
This is taken from Objective C ++ Study Notes: The initialization list members are listed in the same order as they are declared in the class.
Http://www.cppblog.com/xczhang/archive/2008/01/22/41613.html

Why? We know that for all members of an object, the order in which their destructor are called is always the opposite of the order they are created in the constructor. Then, if the above conditions (that is, the members are initialized in the order they appear on the initialization list) are allowed, the compiler will track the initialization order of each object's members, to ensure that their destructor are called in the correct order. This results in expensive costs. Therefore, to avoid this sale, all objects of the same type Process members in the same order during creation (construction) and destruction (analysis, regardless of the Order of the members in the initialization list.

Note: The above content is not applicable to static variables. Static variables should be initialized before the class constructor.

 

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.