C + + Reference essence of Understanding ①

Source: Internet
Author: User

//referential nature of understanding ①#include <iostream>using namespacestd;intGeta () {intA =Ten; returnA;}int&Getb () {intA =Ten; printf ("the address of a is%x\n", &a); returnA;}voidMain () {intA1 =Ten, a2 =0; A1=Geta (); printf ("the value of A1 is%d\n", A1); //Print Ten//After careful observation of the memory discovery return A; the C + + compiler did not immediately destroy the memory space identified by a after the sentence was executed.//instead of executing a1 = Geta (), the memory space is not destroyed after this copy operation//so the variable A1 can accept the value of local variable A to print 10 is not accidental success A2=Getb (); //Pre-description int &b=a; //A reference is essentially a constant pointer B is a pointer, but a reference to pointer B's memory space cannot be changed//C+ + compiler internal completion is ① create an int * const type of constant pointer b      //② assigns the address of variable A to the constant pointer b//when the C + + compiler discovers that there is an operation that requires a value or assignment to (reference pointer) b, the//c++ compiler defaults to B for a fetch *P operation----This is the C + + compiler internal behavior (so C + + will be so slow to do a lot of stealth)//For example printf ("b=%d\n", b);    This is essentially printf ("b=%d\n", *b); //another example b=20;    essentially a *b=20; // again, the default pointer extraction * operation is only a special treatment of C + + reference pointers ; C + + compiler does not default to other pointers * Operation//so let's analyze the function Getb ()//when Getb () return A; is equal to the C + + compiler defines a temporary reference pointer temp//assigns the address of variable A to the temporary reference pointer temp//Execution a2 = Getb ();       is actually executing a2=*temp; //After careful observation found return a; the C + + compiler did not immediately destroy the memory space identified by a after the sentence was executed.//instead of executing a2 = GETB (); The memory space of local variable A is not destroyed after this copy operation//so the a2=*temp at this time; Fully effective//the "=" assignment operation is to copy the value of the local variable A into the memory space identified by the A2printf ("A2 's address is%x\n.", &AMP;A2);//Print a4f754printf"the value of A2 is%d\n", a2);//Print Ten        int&AMP;A3 =Getb (); //likewise for int &a3 = GETB ();    You can essentially write int &a3=*temp; //at this point the *temp is a value because the memory space of local variable A is not released because it executes to int &a3 = GETB ();//Define int &a3=*temp; Then the C + + compiler assigns the *temp address to the reference pointer A3 (A3=TEMP;), which assigns the value of the pointer temp to the pointer A3 //the value of reference pointer temp is also &a (address of a)//when performing printf ("A3 value is%d\n", A3);     Essentially the execution of printf ("A3 value is%d\n", *a3); //but int &a3 = GETB (); The memory space of local variable A is freed after execution is complete .//The data for the memory space pointed to by temp has been reset by the system, so the *temp data can only be dirty data and A3 is equal to temp.//so *A3 data is also dirty data.printf ("A3 's address is%x\n.", &AMP;A3);//Print a4f664 At this point the address of local variable A is also a4f664printf"the value of A3 is%d\n", A3);//Dirty DataSystem"Pause");}

C + + Reference essence of Understanding ①

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.