C ++ uses to initialize the list (summary)

Reference description link  https://www.cnblogs.com/dishengAndziyu/p/10906081.html

 

1, initialization list is in C ++ was introduced;

 

2, "whether the class can be defined const member?" This question is introduced initialization list:

    1, const keyword to define constants in the true sense, read-only variables can also be defined in some cases;

 

3, small experiment:

    1, the following class definition is legitimate? If legitimate, what is the value of ci, where stored?

 

Copy the code
 1 #include <stdio.h>
 2 
 3 class Test
 4 {
 5 private:
 6 const int ci; // const effect obtained after the member variables in C ++ member variables are read-only, read-only member variables are not members of the symbols appear to the left; it is an error message will appear on line 10;
 7 public:
 8 /*
 9 Test () // compiler display here: ci is a const member, is not initialized; so if you want to initialize member variables ci, must be carried out in this line, this time to let the factory initialization list;
10     {       
11 ci = 10; // compiler displays where: ci is a read-only member variable in this class;
12     }
13 */
14 
15 / * Change From the above statement follows * /
16 Test (): ci (10) // ci can be changed after initialization, because ci here is just a read-only member variable, just can not appear to the left of assignments it; we can still modify the way through a pointer ci which value;
17     {
18 // ci = 10;
19     }
20        
21     int getCI() 
22     { 
23         return ci; 
24     }
25 };
26 
27 int main ()
28 {
29 Test t; // if there is no program manually defined above no argument constructor when displayed "uninitialized const member" error; the same time when there is no object defined by class, by compile, explain const member variables can be modified in C ++;
30     
31     printf("t.ci = %d\n", t.getCI());
32     
33     return 0;
34 }
Copy the code

 

4, C ++ provides a list of initialization of member variables are initialized, its grammar rules:

    1, sample code:

1 ClassName :: ClassName (): m1 (v1), m2 (v1, v2), m3 (v3) // with v1, (v1, v2), v3 respectively, m1, m2, m3 initialization; 
2 {
3        // some other initialize operation;
4 }

       1, initialization list should be used in place of the constructor;

       2, defines a list of initialization before the body after the constructor function parameter list;

       3, its role is to initialize the member variables;

    2 Notes (to avoid the bug is important):

       1, the same statement sequence initialization sequence with members of the members;

       2, regardless of the position of the initialization sequence in the initialization list members;

       3, a list of the initialization performed prior to the body of the constructor;

           1, when the constructor function body begins execution, the object has been created, and the implementation of the constructor function body just to initialize the state of our object only;

           2, so that the initialization list since it is used to initialize, it must be executed at the same time our class object is created, and should not be the object has been created to come to a series of initialization, and this is a clear difference , that is, the difference between the initialization and assignment of this difference;

      

5, initialization list of experiments using programming:

Copy the code
 1 #include <stdio.h>
 2 
 3 class Value
 4 {
 5 private:
 6 // int mi = 0; // To initialize the member variables can only be used to initialize the list; it is a member variable assignment in the constructor which is not initialized;  
 7  
 8 you mi;
 9     
10 public:
11     Value(int i)
12     {
13         printf("i = %d\n", i);
14 I = i;
15     }
16     
17 you GETI ()
18     {
19         return mi;
20     }
21 };
22 
23 class Test
24 { 
25 private:
26 /*
27 Value m2 (2); // this way a clear call parameters also have grammatical errors;
28     Value m3(3);
29     Value m1(1);
30 */  
31     Value m2;
32     Value m3;
33     Value m1;
34     
35 public:
36 /*
37 Test () // show no compiler where no argument constructor class value to match the call;
38     {
39     
40     }
41 */
42 Test (): m1 (1), m2 (2), m3 (3) // initialize member variables must be done by initialization list;
43     {
44 printf ( "Test :: Test () \ n"); // initialize list of executed before the constructor function body;
45     }
46 };
47 
48 int main ()
49 {
50     Test t;
51     
52     return 0;
53 }
Copy the code

   

6, const class members:

    1, const members of the class will be allocated space; note here :( only by the number of compile-time initialization immediately is really constant, const member can not direct assignment at compile time, can not enter the symbol table to see compilation can you directly know the value of time)

       1, the same space const member space allocation and distribution of our entire class objects;

    2, the nature of the const member variable classes is read-only;

       1, a bug error message obtained by the compiler provided;

    3, const members of the class can specify the initial value in the initialization list;

       1, the compiler can not directly get the initial value of the const member, and therefore can not enter the symbol table become a constant in the true sense;

           1, because only at runtime to define the object, call the constructor, then call the initialization list to initialize member variables;

      

7, read-only member variable programming experiment:

  1, sample code:

Copy the code
 1 #include <stdio.h>
 2 
 3 class Value
 4 {
 5 private:
 You mi 6;
 7 public:
 8     Value(int i)
 9     {
10         printf("i = %d\n", i);
11 I = i;
12     }
13 you GETI ()
14     {
15         return mi;
16     }
17 };
18 
19 class Test
20 {
21 private:
22 but const int;
23     
24     Value m2;
25     Value m3;
26     Value m1;
27     
28 public:
29     Test() : m1(1), m2(2), m3(3), ci(100)
30     {
31         printf("Test::Test()\n");
32     }
33     
34     int getCI()
35     {
36         return ci;
37     }
38     
Gill-nets 39 int (int v)
40     {
41 int * p = const_cast <int *> (& ci); // pointer through the object to operate a constant;
42         
43 * p = v;
44     }
45 };
46 
47 
48 int main ()
49 {
50     Test t;
51     
52     printf("t.ci = %d\n", t.getCI());
53     
54 t.setCI (10);
55     
56     printf("t.ci = %d\n", t.getCI());
57     
58     return 0;
59 }
Copy the code

    2, this experiment shows:

       1, is not a true constant in the sense const member of a class, it's just a read-only variables (the compiler told);

   

8, episode:

    1, initialization and assigning different:

       1. Initialization: The object is created the initial value;

           1, initialization, when the object has not created at the same time created, we will determine its value;

       2, the assignment: object already present value;

1 int main ()
2 {
3 int i = 0; // this is initialized, when i does not exist;
4     // ...
5 i = 0; // this is an assignment, a i is real, and i has a value, and this time the value of i is changed
6 }

      

9 Summary:

    1, class initialization list can be used to initialize the members;

       1, the class can not directly initialize member variables (variables as whether the object is a general class), it can only be initialized by the initialization list;

    2, the initialization performed prior to a list of the constructor body;

    3, the class may be defined const member variables (here variables);

       1, const acting on the members of the class variables obtained only read-only;

    4, const member variables must specify the initial value in the initialization list;

    5, const member variables are read-only variables;

Guess you like

Origin www.cnblogs.com/lh03061238/p/12312853.html