A composite type refers to a type that is defined based on another type. There are several composite types in the C + + language, two of which are described here: references and pointers.
References
·
·
·
·
Base reference
"Rvalue reference" has been introduced in the C++11 version, and we will not explain it here first. This reference mainly works on built-in types. Strictly speaking, when we use the term "reference", the default is the Lvalue reference.
A reference is like a different name for an object, and a reference type refers to another type. We define the reference by writing the declaration as &d, where D is the declared variable name:
int1024; //定义一个int类型变量 int &refVal = ival; // int类型refVal变量是对ival的一个引用 int &refVal2; //错误! 引用在定义的时候必须被初始化。
·
·
Reference = = Alias
In general, when initializing a variable, the initial value is copied to the newly created object. However, when defining a reference, the program bar reference and its initial value are bound together, rather than copying the initial value to the reference. Once the initialization is complete, the reference will be bound together with his initial value object. because the reference cannot be re-bound to another object, the reference must be initialized.
Once a reference is defined, all operations on it (referred to here) are made on the object to which it is bound.
int1024; //定义一个int类型变量 int &refVal = ival; // int类型refVal变量是对ival的一个引用 2; //等价于ival = 2, 把2赋值给了引用绑定的对象; int ii = refVal; //与ii = ival的调用效果一样
To assign a value to a reference, it is actually an eight-value assignment to the object that is bound to the reference, getting the referenced word, and actually getting the value of the object bound to the reference. In the same vein, a reference is actually the initial value of the object bound to the reference as the initial value.
int1024; //定义一个int类型变量 int &refVal = ival; // int类型refVal变量是对ival的一个引用 int &refVal3 = refVal; // 正确! 这里rafVal3是inval的引用!! int i = refVal; // 正确i被初始化为ival的值;
Here we should note that the reference itself is not an object, so we cannot define a reference reference.
The last code int &refVal3 = refVal; is actually defined int &fefVal = ival in the
·
·
Definitions of references
Allow values. Define multiple references in one statement, each of which must begin with a symbol &;
int10242048; //这里定义两个int类型的变量i、i2 int &r = i, r2 = i2; //这里r是一个引用, 与i绑定在一起, r2是int类型 int1024, &r1 = i3; // i3是int类型, ri是一个引用, 与i3绑定在一起。 int &r3 = i3, &r4 = i2; //r3 和r4 都是引用
In addition to the two scenarios we're going to cover next, all of the other referenced types are strictly matched to the objects that are bound to them. And a reference can only be bound to an object, not to a literal value or to a result of an expression calculation!!
int1024 //错误!只能绑定在对象上double3.14;int &refVal5 = dval //错误!此处引用的类型和初始值的类型必须相同
·
·
·
·
A const reference
We can bind a reference to a const, like binding to another object, which we call a binding to a constant. Unlike a normal reference, a reference to a constant cannot be used to modify the object it is bound to.
constint1024; //定义一个int 类型的常量 a constint &b = a; //定义一个常量引用b 与a绑定 42; // 错误!!!! a是常量不能改变他的值 int &d = a; // 错误 常量只能被常量引用所引用
Because A is a constant, we do not allow to assign a value directly to a, and of course it is not possible to change the value of a by reference. Therefore, the initialization of D is wrong. Assuming that the initialization is legitimate, it is clearly illegal to change the value of the object he refers to by using a const.
Here's a couple more words.
const int &b = a;the const in is the underlying const, which is the qualification of B: Modifying a value by B is illegal
Here we use pointers for example:
int A;
int const *B = A here means that the point of B does not change, only point to a
const int *C = A here means that the value of a cannot be modified by C
·
·
Initialization and reference to const
As we mentioned above, the type of the reference must match the type of the object it refers to, but there are two exceptions. The first exception is to allow any expression to be the initial value when initializing a constant reference, as long as the expression can be converted to a referenced type (that is, a coercion type conversion occurs at the time of reference ), and the other is that a constant reference binds a very high amount object, literal value, or even an expression. Note that both of these special cases are special under constant reference, that is, in the case of the underlying reference, there is no special case.
Steal a lazy Direct:
The simplest way to understand the real reason for this exception is to figure out how a constant reference is bound to another type.
·
·
This time, RI refers to the number of an int type. The operation on the RI should be an integer operation, but at that time Dval was a double-precision floating-point number instead of an integer. So to make sure that Ri binds an integer, the compiler transforms the above code into the following form:
In this case, the RI is bound to a temporary object, and the so-called temporary object is an unnamed object created temporarily by the compiler that requires a space to hold the result of the evaluation of the expression. Temporary variable objects are generally referred to as temporary quantities.
Next, when the RI is not a constant, what happens if a process similar to the above initialization is performed.
If RI is not a constant, if a similar operation is performed, the RI is allowed to be assigned, which alters the value of the object referenced by the RI. notice that the object being bound at this time is a temporary variable instead of a dval. If you want RI to refer to Dval, you will want to change the value of the Dval by RI, or the presence of RI will be meaningless, so it seems that if you do not want to bind the reference to a temporary variable, C + + will classify this behavior as illegal.
·
·
A reference to a const can refer to a variable that is not const
It must be recognized that a constant reference only qualifies the action that the reference class participates in, and that the referenced object itself is not a constant that is not qualified. Because the object can also be a very amount, it allows to change his value by other means;
R2 binding integer I is a legitimate behavior. However, the value of I is not allowed to be modified by R2. However, we can still modify the value of I by other means. You can either assign a value directly to I, or you can modify the value by binding the other reference to I like a person.
The implementation nature of the reference in C + + is the delivery of the address
C + + Learning (2.3) references