1. References to integer variables
The procedure is as follows:
#include <stdlib.h>
#include <iostream>
Using namespace Std;int main () {int A=10;int &b=a; b=200;//Here the variable assignment cannot be int, otherwise redefine cout<< "at this point the value of a is;" <<a<<endl; A=100;cout << "The value of B at this time is:" <<b<<endl;system ("pause"); return 0;}
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/05/75/wKiom1mlXIHhUW_xAAAWeyiihFU379.png "title=" Untitled. png "alt=" Wkiom1mlxihhuw_xaaaweyiihfu379.png "/>
Indicates that the reference simply defines an alias for the variable that already appears. So in the process of program execution, what to do with the original variable and what to do with his only alias is actually a matter of indistinguishable, because essentially, both is a thing.
2. References in structs
#include <stdlib.h> #include <iostream>using namespace std;typedef struct{int x;int y;} The definition of the coord;//struct, which is the name of the struct, the int main () {COORD c;//struct type C. The COORD &c1=c;//data type is the struct name and then by Format & alias c1.x=10;c1.y=29;cout<<c.x<<endl<<c.y<<endl; System ("pause"); return 0;}
Operation Result:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A4/27/wKioL1mlYFKjUKgKAAAGnhScTvg702.png "title=" Untitled. png "alt=" Wkiol1mlyfkjukgkaaagnhsctvg702.png "/>
Note: When a struct is defined and a struct is referenced, the struct name is equivalent to the data type.
3. References in pointers
#include <stdlib.h> #include <iostream>using namespace Std;int main () {int a=10; int *p=&a;//Define a pointer p, It points to a. Note & int *&q=p; The format of the pointer type reference: Data type * & alias = original pointer *q=5;//Note You cannot precede the data type, otherwise there is a duplicate definition cout <<a<<endl; System ("pause"); return 0;}
Operation Result:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/05/77/wKiom1mlbFDTzl7IAAAGOxlioOM071.png "title=" Untitled. png "alt=" Wkiom1mlbfdtzl7iaaagoxlioom071.png "/>
4. The referenced alias as a function parameter
Code:
#include <stdlib.h> #include <iostream>using namespace std;//declares a function with swap function void fun (int &a,int &b) //a,b is the actual parameter alias, used as formal parameter, note the alias must be added data type AH int main () {int x=10;int y=20;cout <<x<< "," <<y<< Endl Fun (x, y);cout<<x<< "," <<y<<endl;system ("pause"); return 0;} void Fun (int &a,int &b) {int c=0;c=a;a=b;b=c;}; Operation Result:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A4/28/wKioL1mlb2Cj3QKzAAALJFYBe3A724.png "title=" Untitled. png "alt=" Wkiol1mlb2cj3qkzaaaljfybe3a724.png "/>
Today we mainly learn several types of reference of C + +, it is easy to understand that there is no reference to the corresponding format type. Today's study ends here.
C + + Reference issues