There are two types of
Declaration statement: A basic data type that consists of a list of declarators
int ival = 1024;int &refval = ival; Refval points to Ival (another name for ival) int &refVal2; Error, reference must be initialized
-
General initialization variables: the initial values are copied to the newly created object
-
Definition reference: binds the reference with his initial value , rather than copying it to a reference. The initialization is complete, and the reference and initial values are bound together . Because the reference cannot be re-bound to another object, the reference must be initialized .
-
References and aliases
-
A reference is not an object, but it is just another name for an already existing object , and after the reference is defined, all operations are performed on the object to which it is bound.
-
Refval = 2; Assigns 2 to the object that Refval points to, namely gives Ivalint II = Refval;
-
Reference assignment: Assigns the value to the object to which it is bound.
-
Gets the value of the reference: Gets the value on the object to which it is bound
-
Use reference as initial value: reference the bound object as the initial value
-
Correct: RefVal13 bound to the object bound to the Refval, is bound to the ival int &refval13 = Refval; Initializes the variable iint i = Refval with the value of the Refval bound object; Correct: I was initialized to the value of ival
-
The reference itself is not an object, so reference references cannot be defined.
-
Definitions of references
-
Allows a statement to define multiple references, each of which must start with &
-
int i = 1024x768, i2 = 2048; I and i2 are int int &r = i, r2 = i2; R is a reference, bound together with I, R2 is intint i3 = 1024x768, &ri = i3; I3 is Int,ri is a reference, bound together with i3 int &r3 = i3, &r4 = i2; R3 and R4 are all references
-
In addition to the two exceptions described in 2.4.1 (55 pages) and 15.2.3 (534 pages), the other referenced types are strictly matched to the objects to which they are bound. A reference can only be bound on an object, not a literal value or the result of an expression, for reasons 2.4.1 detailed.
-
int &refval4 = 10;double Dval = 3.14;int &REFVAL5 = dval;
This article is from the "Mount Sen" blog, please be sure to keep this source http://munson.blog.51cto.com/7358127/1739966
C++primer Notes Composite Type--citation