In layman's terms, a reference (reference) is another name for the object.
1) The reference must be initialized
int &ref; // 错误
int val=10;int &ref2=val; // ref2指向val(val的另一个名字)
When the variable is initialized, the initial value is copied to the newly created object. When a reference is defined, the program binds the reference to its initial value (BIND), rather than copying the initial value to the reference. Once the initialization is complete, the reference is bound together with its initial value object. Because the reference cannot be re-bound to another object, the reference must be initialized.
Add: The so-called object is a piece of data stored and have some type of memory space.
2) A reference is an alias. A reference is not an object, it is simply the name of another object that already exists.
3) The initial value of a reference type must be an object.
int &ref3=10; // 错误
4) You can bind a reference to a const object as if it were bound to a different object, which is referred to as a reference to a constant (reference to const). Unlike a normal reference, a reference to a constant cannot be used to modify the object it is bound to.
constint10;constint &cRef= cVal; // 正确: 引用及其对应的对象都是常量cRef=20; // 错误: cRef是对常量的引用int &ref2= cVal; // 错误: 不能让一个非常量引用指向一个常量对象
5) In addition to the following two exceptions, all reference types are strictly matched to the object to which they are bound.
double dVal=20.0;int %ref4 = dVal; // 错误
The first exception is to allow any expression to be used as the initial value when initializing a constant reference, as long as the result of the expression can be converted to a reference type. "C + + Considerations" 1 data types and type conversions
In particular, it allows for a constant reference to bind a very mass of objects, literals, or even a generic expression.
int i=42;constint &r1=i; // 允许const int&绑定到一个普通int对象上constint &r2=42; // 正确: r2是一个常量引用constint &r3=r1*2; // 正确: r3是一个常量引用int &r4 =r1*2; // 错误: r4是一个普通的非常量引用
The following example shows what happens when a constant reference is bound to another type.
double3.14;constint &ri= dval;
The RI here refers to the number of an int. The operation on the RI should be an integer operation, but Dval is a double-precision floating-point number instead of an integer. So to make sure that Ri binds an integer, the compiler turns the above code into the following form:
int temp= dval;constint *ri= temp;
In this case, the RI is actually bound to a temporary volume (temporary) object. The so-called temporary volume object is the temporary creation of an unnamed object when the compiler needs a space to stage the evaluation result of the expression. Temporary volume objects are often referred to as temporary quantities.
If RI is not a constant, the RI is allowed to be assigned a value, which alters the value of the object referenced by the RI. The object being bound at this time is a temporary amount instead of a dval. Since the dval is referenced by RI, then you want to modify the value of the Dval by RI, otherwise why do you want to assign the RI value? So since you don't want to bind a reference to a temporary amount, the C + + language will classify this behavior as illegal.
6) A reference to a const may refer to an object that is not const. A constant reference only qualifies for the action that can be engaged, and is not a constant for the referenced object itself. So the object can also be a very volume, so it is allowed to change its value by other means.
int42;int &r1= i; // 引用ri绑定对象iconstint &r2= i; // r2也绑定对象i,但是不能通过r2修改i的值0; // r1并非常量,i的值修改为00; // 错误: r2是一个常量引用
R2 binding a very integer i is legal, but I cannot modify the value of I by R2. However, it can be modified by direct assignment or by means of a very good reference.
"C + + considerations" 3 references