One is used to pass the value of the first address is used to obtain the
& (Reference) ==> appears when the variable declaration statement is on the left side of the variable, indicating that the declaration is a reference.
Example: int &rf; Declares a reference RF of type int.
& (address operator) ==> appears on the right side of the equals sign when assigning an initial value to a variable or when it appears as a unary operator in an execution statement
Represents the address of the Fetch object.
In C + +, there are both references and addresses, many people are not very clear about the reference and address, and therefore can not be distinguished. In fact, their differences can be summed up in one sentence: With the type is the reference, and the variables are taken together is the address. Let's take a look at the examples below
1) The reference is to the left of the assignment =, and the address is to the right of the assignment, such as
- int a=3;
- int &b=a; //reference
- int *p=&a; //Take address
2) and the type together is the reference, and the variable is taken together is the address. For example, as well as the following example:
- int function(int &i)
- {
- } //reference
3) for vectors, the above 2 are equally suitable
- Vector<int> VEC1(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
Reference and FETCH address