C ++ has a deep understanding of references.

Source: Internet
Author: User

C ++ has a deep understanding of references.

After reading the lecture "lesson 5th-Essence Analysis of references" explained by Tang, I feel very good. I like it ~~~

In this summary, we will provide the video ~~~

360 Network Disk: https://yunpan.cn/cxXynI6sGbHJs password (4b1b)

// ------------------------------------------------ Body ----------------------------------------------

 

This syntax is not referenced in C, but it is used in C ++. Why does C ++ need to add reference to this syntax?

In my understanding, reference is the encapsulation of pointers!

First, there are many pointer variables: p * p & p;

1) p: represents the address value stored in the pointer variable. This address value is generally the memory address of a variable.

2) * p: corresponds to the value in the memory address stored in p.

3) & p: memory address for storing pointer variables.

Using a pointer means changing the form at any time, such as getting an address and unreferencing it. Sometimes it may be confusing to understand a slight deviation, forget to take the address or unreference it.

In order to simplify the usage of pointers and have the same powerful functions as pointers, the reference will appear.

First look at a program:

Int main (int argc, char * argv []) {// common variable int a0 = 8; int b0 = a0; b0 = 88; cout <a0 <endl; // pointer variable int a1 = 8; int * b1 = & a1; cout <* b1 <endl; * b1 = 88; cout <a1 <endl; // reference int a2 = 8; int & b2 = a2; b2 = 88; cout <a2 <endl; return 0 ;}

 

 

1. b0 is a common variable, int b0 = a0; it is only a simple value assignment, so changing the value of b0 cannot be changed.

A0 values, which are associated with different memory spaces.

2. b1 is the pointer variable, and int * b1 = & a1; this sentence gives the a1 address to the pointer variable b1.

3. b2 is a reference of a2. int & b2 = a2; indicates that b2 and a2 are associated. From then on, they are connected with each other. Comparing this program with a common variable, this program solves one more problem, but achieves the pointer effect. Removes the need for pointer-based address fetch. It seems that two names are given to the same memory space. either of these two names can perform operations on this memory.

After learning about the advantages of reference, I will analyze its principles. Previously, I said that reference is the encapsulation of pointer. In fact, behind the reference, it is actually a pointer, the compiler only hides this detail. How can we prove it?

First, create a struct:

Struct TRef

{

char& r;

};

Then test the size of the struct:

Cout <sizeof (TRef) <endl;

The size is 4, which is exactly the size of a pointer! (Remove & the size of the test is 1). For further analysis, see the assembly.

Char & B = a; after disassembly, it is changed to two sentences:

The first sentence is to enlarge the address of a in the eax register, and then put the value of eax and the address of a in the address space of B, so B is loaded with the address value of. This is the implementation process of pointers!

So once the compiler identifies this variable as a reference, when it associates a variable with this reference, the compiler automatically obtains the address for the associated variable. When it assigns a constant to the reference, the compiler automatically calls this variable.

This is because the compiler automatically retrieves the address and references, so that you do not need to do these error-prone tasks and complete pointer work.

Here, I would like to add a note:

If you test cout <sizeof (char &) <endl;, the value of cout is 1 instead of 4. this is because, if you directly access the reference, the compiler will help you solve the reference process, then you detect char rather than pointer. Put it in the struct to get the reference feature without directly operating the reference.

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.