(Note): initialization order of the initialization list. List Initialization

Source: Internet
Author: User

(Note): initialization order of the initialization list. List Initialization

I. I have already introduced the benefits of the initialization list, especially for class-type variables, which will improve the efficiency. Next I will introduce the situations that must be placed in the initialization list (irrelevant to efficiency issues ):

1. Constant members, because constants (const) can only be initialized and cannot be assigned values (although they cannot be assigned values, they can be accessed and modified through pointers), so they must be placed in the initialization list.
2. The reference type. The reference must be initialized during definition and cannot be re-assigned (this is the usage rule referenced in C ++, which is different from the pointer, it cannot be changed at runtime), so it should also be written in the initialization list.
3. There is no default constructor class type, because you do not need to call the default constructor to initialize using the initialization list, but directly call the copy constructor to initialize. This is recorded in the previous note (list of constructors at the beginning. Because a variable of the class type Automatically calls the default constructor for initialization during definition, when a member variable of the class type does not have a default constructor, it can only be performed in the initialization list. The copy constructor is working. The following code calls one default constructor and value assignment constructor, And the other calls the copy constructor (both cases have been marked in the Code ):

1 # include <iostream> 2 3 using namespace std; 4 5 class Student1 {6 public: 7 int a; 8 9 Student1 () // No parameter constructor 10 {11 cout <"default constructor Student1" <endl; 12} 13 14 Student1 (const Student1 & t1) // copy the constructor 15 {16 cout <"copy constructor Student1" <endl; 17 this-> a = t1.a; 18} 19 20 Student1 & operator = (const Student1 & t1) // value assignment operator 21 {22 cout <"value assignment function Student1" <endl; 23 this-> a = t1.a; 24 return * this; 25} 26}; 27 class Student228 {29 public: 30 31 Student1 test; 32 Student2 (Student1 & t1) {// call the default constructor and value assignment constructor 33 test = t1; 34} 35 // Student2 (Student1 & t1): test (t1) {}// in this case, only the copy constructor is called, 36}; 37 int main () 38 {39 40 Student1 A; // enter the default constructor 41 Student2 B (); // enter the copy constructor 42 43}

Ii. initialization sequence
The initialization sequence should be consistent according to the sequence of variables declared in the class; otherwise, the following special circumstances may occur:
Take the above Code as an example and add the appropriate Code. The Code effect is as follows:

1 # include <iostream> 2 3 using namespace std; 4 5 class Student1 {6 public: 7 int a; 8 int B; 9 void fprint () {10 cout <"a =" <a <"" <"B =" <B <endl; 11} 12 13 Student1 (int I ): B (I), a (B) {}// exception sequence: It is found that the value of a is 0 and the value of B is 2, which indicates that initialization is only effective for B, no initialization effect on a 14 // Student1 (int I): a (I), B (a) {}// normal order: it is found that a = B = 2 indicates that both variables are initialized with 15 16 Student1 () // No parameter constructor 17 {18 cout <"default constructor Student1" <endl; 19} 20 21 Student1 (const Student1 & t1) // copy constructor 22 {23 cout <"copy constructor Student1" <endl; 24 this-> a = t1.a; 25} 26 27 Student1 & operator = (const Student1 & t1) // value assignment operator 28 {29 cout <"value assignment function Student1" <endl; 30 this-> a = t1.a; 31 return * this; 32} 33 34}; 35 class Student236 {37 public: 38 39 Student1 test; 40 Student2 (Student1 & t1) {41 test = t1; 42} 43 // Student2 (Student1 & t1): test (t1) {}44}; 45 int main () 46 {47 48 Student1 A (2); // enter the default constructor 49 Student2 B (A); // enter the copy constructor 50. fprint (); // output the initialization Result 51 52}

The two initialization methods are as follows:

Exception initialization sequence:

Normal initialization sequence:

The above example shows that the initialization list sequence must be consistent with the class declaration sequence. Otherwise, some variables will not be initialized in the special case above. After testing, it is found that the variables in the class can be initialized normally as follows: That is, as long as the initialization of the member variables does not depend on other member variables, they can be correctly initialized even if the order is different.

1 int a;2 int b;3 int c;4 Student1(int i):b(i),a(i),c(i){} 5 main:6 Student1 A(2);    7 A.fprint();

Result:

1 int a;2 int b;3 int c;4 Student1(int i,int j,int k):b(i),a(j),c(k){}5 main:6     Student1 A(2,3,4);7     A.fprint();

Result:

 

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.