1. Pointers, references, and const
1 int i= 42 2 int &a = I; 3 p ; 4 // r is a reference to the pointer 5 r=&i; // 6 *r=0 ; // 7 a =3 ;
A reference is essentially an individual name for a variable, which is not itself an object, but is tied to the initial value. So he must initialize it when he defines it, and cannot replace it with another object, and there is no reference to it. A normal "extraordinary reference" can reference only one object, cannot reference a constant, a const-qualified "constant reference" may refer to a constant, or a common variable. (Here "constant reference" is simply a short name for a const reference, there is no constant reference, because the reference is not an object)
1 int i =1; 2 Const int &a = i; 3 3; // this sentence is right. And the value of a also changes 4 a =2// The sentence is wrong. 5 cout<<a;
The pointer itself is an object, so it can be copied and assigned, and can point to different objects, which can be uninitialized when the pointer is defined, that is, nullable. You can have pointers pointing to pointers.
There is a reference to the pointer, and there is no pointer to the reference. The pointer is an object so there is a constant pointer. As follows:
const int *p = &i;// pointer to constant int const *p = &i; // same as above int *const q = &i;// *p = 1 ; // error, p points to a constant that cannot be changed. p = &c; // correct, P's address can be replaced by *q = 1 ; // correct q = &c; // error, P's address cannot be changed.
2, pointer parameters, array parameters, reference parameters
The pointer behaves like any other non-reference type, and when the pointer copy operation is performed, the value of the pointer is copied, and two pointers are not a pointer after the copy.
1 voidFunint*2) {2*p=0;3p=0;4 }5 6 inti = the;7 int*p = &i;8cout<<p<<endl<<*p<<Endl;9 Fun (p); Tencout<<i<<endl<<p<<endl<<*p;
Output results
You can see that the value of the pointer has changed, but the pointer itself has not changed.
A reference to a parameter is actually a reference, an operation or itself, so changing its value in a function changes the function outside. Because they are all themselves.
An array parameter that does not allow copying an array, and the array is converted into pointers. We are actually passing pointers to the first element of the array.
2, extern and static, const
Some notes from C + +