(1) int
Void swap (int A, int B) {int temp = A; A = B; B = temp;} int A = 1, B = 2; swap (A, B ); cout <A <"," <B <Endl; // 1, 2 is not exchanged
(2) int *
Void swap (int * a, int * B) {int temp = * A; * A = * B; * B = temp;} int A = 1, B = 2; swap (& A, & B); cout <A <"," <B <Endl; // 2, 1 Exchange
(3) Int &
Void swap (Int & A, Int & B) {int temp = A; A = B; B = temp;} int A = 1, B = 2; swap (, b); cout <A <"," <B <Endl; // exchange
(4) int *&
Starting from the symbol closest to value, it is a &, indicating that value is a reference. What is it a reference? Again, * is a pointer reference, that is, the alias of the pointer. We can use * value to access the value of the real parameter. Therefore, the internal logic of the SWAp function is the same as that of int *.
Void swap (int * & A, int * & B) {int temp = * A; * A = * B; * B = temp;} int A = 1, B = 2; int * aptr = & A; int * bptr = & B; int * & arptr = aptr; int * & brptr = bptr; swap (arptr, brptr ); // swap (a, B); cout <A <"," <B <Endl; // 2, 1 Exchange
(5) Int &*
This definition is not legal. If we analyze it literally, for example, Int & * value, which is closest to value *, it indicates that value is a pointer. Then we can look at the previous one &, it indicates a pointer reference, and the pointer cannot be referenced. Therefore, such a definition is invalid.
(6) int **
Void swap (INT ** A, int ** B) {int temp = ** A; ** A = ** B; ** B = temp ;} int A = 1, B = 2; int * aptr = & A; int * bptr = & B; int ** arptr = & aptr; int ** brptr = & bptr; swap (arptr, brptr); cout <A <"," <B <Endl; // 2, 1 Exchange
(7) const
/* There is no difference between putting const before or after the type and INT const a1 = 1 const on the left of *, then the value of the variable pointed to by the pointer is unchangeable; on the right, the Pointer Points are unchangeable. The description is "Left value, right direction ". The pointer operator * is from right to left. For example, char const * Pcontent can be interpreted as char const (* Pcontent), that is, * Pcontent is const, while Pcontent is variable */const int a1 = 1; // set value const int * A2; // set value (pointer variable) const int * const a3 = & A1; // pointer, value // special multi-statement int const * P1, P2; // P2 is const; (* P1) is a whole, SO (* P1) is const, however, P1 is variable. Int * P1, P2 only indicates that P1 is a pointer to an integer. To indicate that P1 and P2 are pointers, you must write them as int * P1, * P2. Therefore, * in * const P1, P2, or const * P1, P2 belongs to P1. Int const * const P1, P2; // P2 is const, which is modified by the previous const, * P1 is also modified by the previous const, and P1 is modified by the last Const. Int * const P1, P2; // P1 is const, (* const P1) is overall, so const does not modify P2. // Example // 1) the value of the variable pointed to by the pointer cannot be changed, pointing to the variable int x = 1; int y = 2; const int * PX = & X; int const * PX = & X; // the two expressions have the same effect as those of PX = & Y; // correct. allowed to change the pointer to * PX = 3; // error, you cannot change the value of the variable pointed to by the pointer. // 2) the value of the variable pointed to by the pointer can be changed, pointing to an immutable int x = 1; int y = 2; int * const PX = & X; PX = & Y; // error. The pointer cannot be changed to * PX = 3; // correct, you can change the value of the variable pointed to by the pointer. // 3) the value of the variable pointed to by the pointer is immutable, pointing to immutable int x = 1; int y = 2; const int * const PX = & X; int const * const PX = & X; PX = & Y; // the pointer cannot be changed to * PX = 3; // error. The value of the variable pointed to by the pointer cannot be changed.