Reference and FETCH address

Source: Internet
Author: User

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

    1. int a=3;
    2. int &b=a; //reference
    3. 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:

    1. int function(int &i)
    2. {
    3. } //reference

3) for vectors, the above 2 are equally suitable

      1. Vector<int> VEC1(1); //initialize vec1:10 elements, every element ' s value is 1
      2. Vector<int> &vec2 = VEC1; //VEC2 is reference to VEC1
      3. Vector<int> *vec3 = &vec2; //VEC3 is addresss of VEC1 and vec2

Reference and FETCH address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.