C ++ reference learning
0x01: simple reference
C language is not referenced, c ++ is referenced, and programming is simplified
Reference is essentially a pointer. An alias is given to an existing variable. sizeof is used to evaluate the size of the referenced variable.
The reference must be initialized at the beginning. Once initialized, the value assignment modification will be invalid;
Int num = 10;
Int & rnum (num); // initialize the variable with parentheses
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:
2. cpp:
1 struct MyStruct 2 {3 void print () 4 {5 printf ("hello world"); 6} 7}; 8 struct MyStructM 9 {10 char & Counter; // 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, indicating the existence of 17 // struct size. The null struct is a byte, 18 // sizeof struct exists, not included in the Code Area 19 cin. get (); 20}
Declared 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; // get the address. If there is no type specifier, It is get the address.
1 double db1 = 10.9; 2 double db2 = 9.8; 3 double * p (& db1); // defines the pointer to initialize 4 5 double * (& rp) (p ); // define the referenced pointer. Use the pointer to initialize 6 7 double * (& rrp) (rp); // you can use another reference to initialize 8
The reference can be initialized with another reference.
0x02: Advanced reference
Reference an array:
1 // int a2 // int & ra; 3 // 1, dig out the variable name, 2, add &, 3 alias 4 int a [5] = {1, 2, 3, 4, 5}; // 5 int (& ra) on the stack [5] (); // reference array 6 int * p = new int [5] {6, 7, 8, 9, 10}; // stack 7 int * (& rp) (p ); // reference the 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); // first, dig out the array name and second (& + alias)
Any array type reference:
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, form parameter, transfer reference, transfer address 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: Ultra-compact reference
Function pointer reference:
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 the 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 a, 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 a, 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: Right reference
C ++ 11, reference the right value
// If it is not the left value, manually copy the object to the memory before reference
// We do not need to copy the file. If the right value is referenced, the compiler automatically copies the file to the memory/
1 void print (int & rint) // reference the right value. Once the memory is referenced, the compiler 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}
// If it is not the left value, manually copy the object to the memory before reference
// We do not need to copy the file. If the right value is referenced, the compiler automatically copies the file to the memory/