One is used to pass the value, and the other is used to obtain the first address.
& (Reference) ==> when the variable declaration statement is on the left of the variable, it indicates that the reference is declared.
For example: Int & RF; // declare an int type reference RF.
& (Obtain the address operator) ==> when an initial value is assigned to a variable, it appears on the right of the equal sign or as a unary operator in the execution statement.
The address of the object.
In C ++, there are both references and fetch addresses. Many people are not very clear about the references and fetch addresses, so they cannot distinguish between them. In fact, their differences can be summarized in one sentence: the type is referenced together, and the variable is accessed together. Next, let's take a look at the examples
1) The reference is on the left of the value =, and the address is on the right of the value, for example
- Int A = 3;
- Int & B = A; // reference
- Int * P = & A; // get the address
2) references are associated with types, and addresses are associated with variables. The example is also as follows:
- Int function (Int & I)
-
- {
-
- } // Reference
3) For vector, the above two entries are equally suitable.
- Vector <int> vec1 (10, 1); // initialize vec1: 10 elements, every element's value is 1
- Vector <int> & vec2 = vec1; // vec2 is reference to vec1
- Vector <int> * vec3 = & vec2; // vec3 is addresss of vec1 and vec2
Difference between reference and address fetch