C + + Initialization list

Source: Internet
Author: User

What is a list of initialization

Unlike other functions, constructors can have initialization lists in addition to names, argument lists, and function bodies, and initialization lists begin with a colon followed by a series of comma-delimited initialization fields. In C + +, the only difference between struct and class is that the default access is different, and here we do not consider the problem of accessibility, so the following code is shown as a struct.

struct foo{    string  name;     int ID;    Foo (stringint//  initialization list };
Two execution stages of a constructor

The execution of a constructor can be divided into two phases, the initialization phase and the calculation phase, and the initialization phase precedes the calculation phase.

Initialization phase

Members of all class types are initialized during the initialization phase, even if the member does not appear in the initialization list of the constructor.

Calculation phase

Typically used to perform assignment operations within the constructor body, the following code defines two structs, where Test1 has constructors, copy constructors, and assignment operators to facilitate viewing of the results. Test2 is a test class that takes a Test1 object as a member, so let's take a look at how Test2 's constructor performs.

structtest1{Test1 ()//no parameter constructor{cout<<"Construct Test1"<<Endl; } Test1 (Consttest1& T1)//copy Constructor{cout<<"Copy Constructor for Test1"<<Endl;  This->a =t1.a; } Test1&operator= (Consttest1& T1)//Assignment Operators{cout<<"Assignment for Test1"<<Endl;  This->a =t1.a; return* This; }    intA;};structtest2{Test1 test1; Test2 (Test1&T1) {test1=T1; }};

Calling code

Test1 T1; Test2 T2 (T1);

Output

Explain that the first line of output corresponds to the first line in the calling code and constructs a Test1 object. The second line outputs the code in the Test2 constructor, initializes the object test1 with the default constructor, which is known as the initialization phase. The third line outputs the assignment operator for the Test1 and performs an assignment on the Test1, which is called the compute phase.

Why use an initialization list

There are two ways to initialize a member of a class, one is to use an initialization list, and the other is to assign a value in the constructor body. The use of initialization lists is primarily based on performance issues, and for built-in types such as int, float, and so on, using the Initialize class table and initializing in the constructor body is not very different, but for class types it is best to use the initialization list, why? As the above test shows, the process of invoking the default constructor one less time with the initialization list is very efficient for data-intensive classes. Also see the example above, we use the initialization list to implement the Test2 constructor.

struct test2{    Test1 test1;     &T1): test1 (t1) {}}

Using the same calling code, the output is as follows.

The first line of output corresponds to the first line of the calling code. The second line outputs the initialization list of the corresponding Test2, invoking the copy constructor directly to initialize the Test1, eliminating the process of invoking the default constructor. So a good rule is to use the initialization list whenever possible using the initialization list.

What must be placed in the initialization list

In addition to performance issues, it is necessary to initialize the list in some situations where the initialization list must be used

    • Constant members, because constants can only be initialized and cannot be assigned, so they must be placed in the initialization list
    • Reference type, the reference must be initialized at the time of definition and cannot be re-assigned, so it is also written in the initialization list
    • there is no default constructor for the class type , because using an initialization list can be initialized without calling the default constructor, but instead calling the copy constructor directly. (Note: This is very important, very useful)

For classes without a default constructor, let's look at an example.

struct test1{    Test1 (int  a): I (a) {}    int  i;}; struct test2{    Test1 test1;     &T1)    {        = t1;    }};

The above code cannot be compiled because the Test2 class is Test1 test1; The default constructor needs to be called, but the Test1 class does not have a parameterless constructor, but because Test1 does not have a default constructor, it compiles incorrectly. The correct code is as follows, using an initialization list instead of an assignment operation.

struct test2{    Test1 test1;     &T1): test1 (t1) {}}
Initialization order of member variables

members are initialized in the order in which they appear in the class, rather than in the order in which they appear in the initialization list , as seen in the code.

struct foo{    int  i;     int J;    Foo (int//  OK, first initialize I, post-initialize J};

And look at the code below.

struct foo{    int  i;     int J;     foo (int//  I value not defined  };

The value of I here is undefined, although J appears in the initialization list in front of I, but I is defined before J, so I was initialized first, but I was initialized by J, at this time J is not initialized, so the value of I is undefined. Therefore, a good habit is to initialize in the order in which the members are defined.

Transferred from: http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html

C + + Initialization list

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.