Simple record of initialization list in C + +

Source: Internet
Author: User

First place a link to another blogger: http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html

In the process of learning C + + constructors encountered the Word initialization list, and then actively search for information, learning the concept of initialization list

First, the origin of the initialization list

In constructors, the initialization list is not required except for the function name, the function body, the initialization list, or the initialization list .

struct Test

{

Public

Test (string n, int b): Name (n), id (b) {};//The representation of the initialization list: The initialization list begins with a colon followed by a series of comma-delimited initialization fields

Test (string n, int b)

// {
name = N;

id = b;

} General Constructors

Private

String name;

int id;

};

In the constructor, the constructor assigns a value to the member variable in two steps, the first is the initialization phase, the second is the calculation phase, and the initialization phase is before 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. The member that is meant to be initialized is not in the initialization list, and the corresponding constructor is called for initialization, such as the default constructor, the generic parameter constructor, the copy constructor, the type conversion constructor, and so on.

Calculation phase:

Typically used to perform assignment operations within the constructor body

The assignment operation I understand is the operation inside the function body of the constructor,

struct Test1 {

Test1 ()//No parameter constructor

{cout << "Construct Test1" << Endl;

}

Test1 (const test1& T1)//copy constructor

{cout << "Copy constructor for Test1" << Endl;

This->a = t1.a;

}

test1& operator = (const test1& t1)//assignment operator

{

cout << "Assignment for Test1" << Endl;

This->a = t1.a;

return *this;

}

int A;

};

struct TEST2 {

Test1 test1; Member variables

Test2 (Test1 &t1)//constructors

{test1 = T1;

}

};

Execute code:

Test1 T1; The default constructor for Test1 is called, printing Construct Test1

Test2 (t1); Print construct Test1: Because Test2 has a member variable of test1, the Test1 default constructor is called during its initialization phase.

Printing assignment for Test1 is an overload of the = number that is assigned to the constructor phase of the call to Test2.

Ii. How member variables are initialized

1, initialized with initialization list, initialization list is used to initialize member variables.

2, in the constructor, with the assignment of the initialization of the statement.

Iii. What are the benefits of initializing a list?

The use of initialization lists is primarily based on performance issues, and the process of invoking the default constructor one less time with the initialization list is faster when there are many member variables. Fast is the hard way.

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

In this case, the TEST2 initialization list, which calls the copy constructor directly to initialize the Test1, will not invoke the Test1 default constructor. This is more efficient.

Iv. those that must be initialized with the initialization list

1, constant members, because constants can only initialize can not be assigned value, so must be placed in the initialization list

2, reference types, references must be initialized at the time of definition, and cannot be re-assigned, so it is also written in the initialization list.

3, there is no default constructor for the class type, because using the initialization list can not be initialized by calling the default constructor, but directly invoking the copy constructor initialization.

Note: The type of a class without a default constructor calls the default constructor without initializing the list, but it does not have a default constructor, and it will error. ----Billadd

Daniel concludes:

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

2. If there is a const modifier in a class member, when the object is initialized, it must be assigned to the const int m when the Worth class member contains a const object, or a reference, they must be initialized by the member initialization list, because both objects are initialized immediately after the declaration. In the constructor, it is the assignment to them, which is not allowed.

Example:

struct TEST2
{
Test1 test1;

Test1 &aaa;
const int m;
Test2 (Test1 &t1): test1 (T1), M (Ten), AAA (test1) {}
};

V. Sequence of initialization of variables in a class

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.

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

Personal Summary:

1. Constructors can have initialization lists, or they can have no initialization list

2, the constructor is divided into initialization and calculation of two stages, the initialization phase in front, the calculation phase behind.

3. Initializing a variable with an initialization list is faster than a normal assignment function because it eliminates the call to the default constructor.

4. Classes with constant volume, reference, and no default constructors must be initialized with an initialization list.

5. The order in which the lists are initialized is initialized according to the order in which they appear in the class.

Remark: Dm10_init_list.cpp

Simple record of initialization list in C + +

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.