Introduction to C + + references:
The reference introduces a synonym for the object. Defining a reference is similar to defining a pointer, but instead of "*" with &. The reference is an important extension of C + +.
A reference is an alias for a variable, and the action on the reference is exactly the same as the direct operation on the variable.
Declarative method of reference: type identifier & reference name = Target Traversal name
Example Description:
int A; int &p_a=a;
Note: (1):& is not an address operation, but an identification function.
(2): The type identifier refers to the type of the target variable.
(3): a declaration reference, which must be initialized for its process.
(4): When the reference declaration is complete, the target variable name is equivalent to two names, that is, the target name and reference name, and the reference name can no longer be used as an alias for the other variable names.
(5): Declares a reference, does not define a variable, simply means that the reference name is an alias of the variable name, it is not a data type in itself, so the reference itself does not occupy storage space, the system does not assign a storage unit to the reference. Therefore: the reference to the address, is the reference to the target variable on the address.
(6): The reference to the array cannot be established. Because an array is a collection of several elements, you cannot create a
An alias for an array.
Note:
Example: int &p_a[3]={2,3,5};
But it can be written like this: const int (&p_a) [3]={2,3,5}; But GCC compiles with-std=c++0x
Citation Essential Anatomy:
The reference is actually a constant pointer in C + +. the expression int &i=j = = int *const i=&j; and the reference is initialized because the const type variable must be initialized, and the pointer must have a value.
int main () {int i=10; int &j=i; j + +; cout<<i<<j<<endl; cout<<&i<<&j<<endl; return 0; }
Here 's a look at the code:
int main () {int i=10; int *const j=&i; (*j) + +; cout<<i<<*j<<endl; return 0; }
Printing will skip the print address step. This requires some explanation. Because references to variables are automatically dereferenced by the compiler.
References and const