C ++ reference the declaration method and reference the declaration method
A reference is an alias of a variable. A reference operation is an action to the target.
The referenced declaration method:
Type identifier & reference name = target variable name;
For example:
1 int a; 2 int & ra = a; // Define reference ra, which is a reference of variable a, that is, alias
Note:
- & Here, it is not an address operator but an identifier. The identifier declares 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 has two names: the original name and reference name of the target;
- Declaring a reference is not a new variable. It only indicates that the reference name is an alias of the target variable name. Therefore, the system does not allocate a storage unit to the reference.
I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!
Example: Definitions and usage of references.
1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 int a = 3; 7 int & B = a; 8 int & c =; // a variable can have multiple references 9 int & d = B; // reference the initialization reference 10 cout <B <endl; 11 cout <c <endl; 12 cout <d <endl; 13 return 0; 14} 15 16 // cainiao. Please criticize and advise, write code habits and specifications, and so on !!! Thank you !!!
I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!