A reference (reference) is the equivalent of another name for the object. The type refers to a different type. The reference identifier begins with "&".
1 int val =; 2 int &reference = val; // Refval point to Ival 3 int &mistake; // Error: Reference must be initialized
From the above example, you can see that the initial value is copied to the newly created object when the variable is initialized. When you define a reference, the program binds the reference to its initial value. Once the initialization of the reference is complete, the binding relationship will persist, so the reference must be initialized .
Note that a reference is an alias , and it is not an object.
1 int Ten ; 2 int &refi = i; 3 1; // I was assigned to the 1 4 int a = refi; // equivalent to make a = i
The initial value of the reference must be an object, and the referenced type must be strictly consistent with its object, so:
1 int &ref0; // Error 2 Double 1.12 ; 3 int &refa = A; // Error
[001] Initial reference