C + + Learning record (1)--Reference

Source: Internet
Author: User

#include <iostream>#include<stdlib.h>using namespacestd;/*through this program can be found that the reference reference changed, the original value will change, * so when defining the function, if you want to change the actual variable, the form * parameter can be defined as a reference form. It is also important to note that here the & is not * is the address symbol in C, but the meaning of the reference. */ intMainintargcChar**argv) {        //define two variables    intx = -; inty = $; //Create a reference for two variables    int&AMP;X1 =x; int&y1 =y; //to establish a reference for a reference again    int&AMP;X2 =X1; int&y2 =Y1; cout<<"before swapping variables:"<<Endl; cout<<x<<","<<y<<Endl;    Swap (x, y); cout<<"swap (x, y)"<<Endl; cout<<x<<","<<y<<Endl;    Swap (X1,Y1); cout<<"Swap (X1,Y1)"<<Endl; cout<<x<<","<<y<<Endl;    Swap (X2,Y2); cout<<"Swap (X2,Y2)"<<Endl; cout<<x<<","<<y<<Endl; System ("Pause");}//exchanging two of variables by referencevoidSwapint&x,int&y) {inttemp; Temp=x; X=y; Y=temp;}
#include <stdlib.h>#include<iostream>/*pointer reference in order to not be dizzy, the int *p can be written int* p, so that int* as a data type, * next to create a reference after the int* after the variable name plus & OK*/using namespacestd;intMainintargcChar**argv) {    intx = -; int* p = &x; int* &q =p; cout<<q<<Endl; return 0;}

C + + Learning record (1)--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.