#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&X1 =x; int&y1 =y; //to establish a reference for a reference again int&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