C + + in the reference & and Access & is a lot of beginners often error-prone places, today in this article on the analysis and summary for your reference.
In particular, one is used to pass the value of the first address is used to get the
& (Reference) ==> appears in a variable declaration statement at the left of the variable, indicating that the reference is declared.
For example:
int &rf; Declares a reference RF of type int
The & (take address operator) ==> the address of an object when it is assigned an initial value to an equal sign or when it appears as a unary operator in the execution statement.
In C + +, both the reference and the address, many people to the reference and address is not very clear, and therefore can not distinguish. In fact, their differences can be summed up in one sentence: and type together is a reference, and variables together is the access. Let's take a look at the concrete examples below
1 refers to the left of the assignment =, and the address is to the right of the assignment, for example:
int a=3 int
&b=a; /reference
int *p=&a; Take address
2 and type together is a reference, and variables together is the access. For example, as well as the following example:
int function (int &i)
{
}//reference
3 for vectors, the above 2 is equally suitable
Vector<int> VEC1 (10,1); Initialize vec1:10 elements, every element ' s value is 1
vector<int> &vec2 = vec1;//VEC2 are reference to VEC1
vector<int> *vec3 = &vec2;//vec3 is addresss of VEC1 and VEC2
I hope this article will help you with the learning of C + + programming.