Difference between reference and address fetch

Source: Internet
Author: User

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

  1. Int A = 3;
  2. Int & B = A; // reference
  3. Int * P = & A; // get the address

2) references are associated with types, and addresses are associated with variables. The example is also as follows:

  1. Int function (Int & I)
  2.  
  3. {
  4.  
  5. } // Reference

3) For vector, the above two entries are equally suitable.

    1. Vector <int> vec1 (10, 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

Difference between reference and address fetch

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.