C ++ reference learning
0x01: simple references are not referenced in c language, and c ++ has references. The essence of simplified programming references is pointer, which creates an alias for an existing variable, sizeof is used to calculate the size of the accessed variable int num = 10; int & rnum (num); // The variable reference should be properly initialized with brackets 1.cpp: 1 void main () 2 {3 double db (10.9); 4 double & rdb (db); // The reference must be initialized at the beginning of 5 double dbone (10.8); 6 rdb = dbone; // once the reference is initialized, the code can be compiled, and the value assignment modification is invalid. 7 rdb = 8.9; 8 cout <"db =" <db <"dbone =" <dbone <endl; 9 cout <"dbsize =" <sizeof (db) <"rdbsize =" <sizeof (rdb) <endl; 10 cin. get (); 11} 1. cpp running result: the value of dbone has not changed, // The reference must be initialized from the beginning. Once initialized, the value assignment modification will be invalid; 2.cpp: 1 struct MyStruct 2 {3 void print () 4 {5 printf ("hello world"); 6} 7}; 8 struct MyStructM 9 {10 char & character; // reference is essentially an address 11 }; 12 13 void main () 14 {15 cout <sizeof (MyStructM) <endl; // the essence of the reference is that the pointer result is 416 cout <sizeof (MyStruct) <endl; // The result is 1, which indicates that there is 17. // the size of the struct. If the empty struct is a byte, it indicates that there is 18 // sizeof structure. The content is not included in the Code partition. 19 cin. get (); 20} the referenced declaration method: type identifier & reference name = target variable name; 1 double db (10.9); 2 double & rdb (db ); // type identifier & reference name = target variable name 3 cout <& db <& rdb <endl; // obtain the address, if no data type is specified, the address 1 double db1 = 10.9; 2 double db2 = 9.8; 3 double * p (& db1 ); // define the pointer to initialize 4 5 double * (& rp) (p); // define the referenced pointer and use the pointer to initialize 6 7 double * (& rrp) (rp ); // reference can be initialized with another reference. 8 reference can be initialized with another reference. 0x02: Advanced reference references an array: 1 // int a2 // int & ra; 3 // 1, dig out the variable name, 2, plus &, 3 alias 4 int a [5] = {1, 2, 3, 4, 5 }; // 5 int (& ra) [5] (a) on the stack; // reference array 6 int * p = new int [5] {6, 7, 8, 9, 10}; // stack 7 int * (& rp) (p); // reference heap, you can reference the pointer to 8 1 int * p [5] = {& a [0], & a [1], & a [2], & a [3], & a [4]}; 2 3 int * (& rp) [5] (p); // The array name is first mined, and the second (& + alias) reference of any array type: 1 void main () 2 {3 int a [3] [3] = {1, 2, 3}, {4, 5, 6 }, {7, 8, 9} 4}; 5 6 int * p [3] [3] = {& a [0] [0], & a [0] [1], & a [0] [2], 7 & a [1] [0], & a [1] [1], & a [1] [2], 8 & a [2] [0], & a [2] [1], & a [2] [2]}; 9 10 // int * (& rp) [3] [3] (p ); 11 // reference of any array type 12 // first dig out variable name 13 // second add () 14 // third plus & 15 // fourth plus referenced variable name 16 int * (& rp) [3] [3] (p); 17 18 for (int I = 0; I <3; I ++) 19 {20 for (int j = 0; j <3; j ++) 21 {22 cout <(void *) rp [I] [j] <"<* rp [I] [j] <endl; 23} 24} 25 cin. get (); 26} references can break through the copy mechanism: 1 double db1 = 10.9; 2 double db2 = 9.8; 3 4 void change (double * & p) // break through the copy mechanism, parameter. It is referenced. The address is 5 {6 p = & db2; 7} 8 9 10 void main () 11 {12 double * p (& db1 ); // define the pointer to initialize 13 double * (& rp) (p); 14 change (rp ); // The actual parameter passed reference format parameter cannot be changed because the reference is not successful 15 cout <* p <endl; // The result is 9.816 17 cin. get (); 18} 0x03: reference of a super-trivial reference function pointer: 1 void print (char * str) 2 {3 cout <str <endl; 4} 5 6 void main () 7 {8 void (* p) (char * str) (print); 9 p ("hello world "); 10 11 void (* & rp) (char * str) (p); // function pointer reference 12 cin. get (); 13} reference struct array: 1 struct MyStruct 2 {3 int a; 4 int & ra; 5 6}; 7 8 void main () 9 {10 int num [3] {1, 2, 3}; // initialize array 11 MyStruct st [3] {10, num [0]}, {20, num [1] },{ 30, num [2] }}; // initialize the struct array 12 MyStruct * p = new MyStruct [3] {10, num [0] },{ 20, num [1] },{ 30, num [2] }}; // stack 13 14 MyStruct * (& rp) (p ); // reference struct pointer 15 16 MyStruct (& rst) [3] (st); // reference struct array 17 for (int I = 0; I <3; I ++) 18 {19 cout <rst [I]. a <"" <rst [I]. ra <endl; 20 21} 22 for (int I = 0; I <3; I ++) 23 {24 cout <rp [I]. a <"" <rp [I]. ra <endl; 25 26} 27 cin. get (); 28} array of referenced function pointers: 1 int add (int a, int B) 2 {3 return a + B; 4} 5 int sub (int, int B) 6 {7 return a-B; 8} 9 int mul (int a, int B) 10 {11 return a * B; 12} 13 int divv (int, int B) 14 {15 return a/B; 16} 17 18 void mainY () 19 {20 // function pointer array int (* p [4]) (int, int); 21 int (* p [4]) (int, int) {add, sub, mul, divv}; 22 23 int (* (& rp) [4]) (int, int) (p); // reference the function pointer array 24 for (int I = 0; I <4; I ++) // subscript loop 25 {26 cout <rp [I] (100, 10) <endl; 27} 28 for (int (** pp) (int, int) = p; pp <p + 4; pp ++) // pointer loop 29 {30 int (** & rpp) (int, int) (pp ); // reference level-2 function pointer 31 cout <(* rpp) (100, 10) <endl; 32 33} 34 cin. get (); 35 36} 0x04: the right value references C ++ 11 and the right value. // if it is not the left value, manually copy it To the memory entity first, can reference // we do not need to copy, the right value reference, the compiler will automatically help us copy to the memory/1 void print (int & rint) // right value reference, the compiler automatically maintains 2 {3 cout <rint <endl; 4 cout <(void *) & rint <endl; 5 cout <"\ n"; 6} 7 8 9 void main () 10 {11 int x = 30; 12 print (x + 1 ); 13 14 cout <"\ n"; 15 print (x + 2); 16 17 cout <"\ n"; 18 print (x + 3 ); 19 20 cout <"\ n"; 21 print (x + 4); 22 cin. get (); 23 24} 1 void main () 2 {3 int x = 3; 4 print (x + 1); 5 int & rint (x + 3 ); // & reference the Temporary Variable 6 cout without memory entities <rint <endl; 7 cout <"\ n "; 8 9 cout <rint <endl; 10 cout <(void *) & rint <endl; 11 cin. get (); 12}