A reference is an alias for a variable, and the action on the reference is the action on the target.
Declarative methods for referencing:
Type identifier & reference name = target variable name;
Such as:
1 int A; 2 int &ra=a; // defines the reference RA, which is a reference to the variable A, which is the alias
Description
- & Here is not the address operator, but the identity function, where the identity is declared a reference name;
- The type identifier refers to the type of the target variable (that is, a of the above code);
- When declaring a reference, it must be initialized at the same time;
- After the reference declaration is complete, the target variable name is equal to two names, that is, the target name and the reference name;
- Declaring a reference is not a new definition of a variable, it only means that the reference name is an alias of the target variable name, so the system does not assign a storage unit to the reference.
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
Examples: definitions and use of references.
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 intA=3;7 int&b =A;8 int&c = A;//A variable can have multiple references9 int&d = b;//Reference Initialization ReferenceTencout << b <<Endl; Onecout << C <<Endl; Acout << D <<Endl; - return 0; - } the - //Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!!
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
Declaration method of C + + reference