Speaking of references, let me first tell a story: in the yard where we live, there is a little kid named Luo XX. We like to give him a nickname. Seeing that he was not well-nourished, with a relatively big head and a relatively small body, we gave him the nickname "Radish Head". So Luo XX is his real name, and Radish Head is his alias. So what is a reference??

Definition:Reference: it is an alias of a variable.

Note:As far as an alias is concerned, a variable cannot have only an alias without its own real name.

References to Basic Data Types

As in the following piece of code:

Example

#include <iostream> using namespace std; { int a = 3; int &b = a; //b is a reference to a, i.e., b is an alias of a. //A reference must be initialized, otherwise the compilation will report an error b = 10; cout<< a << endl; //At this point, the value of a has changed from the original 3 to 10, because no matter what we do with the alias, we are actually operating on the variable itself. Just like if we ask Radish Head to do something, Luo XX himself will do it. return 0; }

References to Structure Types

As in the following piece of code:

Example

#include <iostream> using namespace std; //Define a structure named Coor, which is a coordinate. typedef struct { int x; int y; }Coor; int main(void) { Coor c1; //Define a structure variable named c1. Coor &c = c1; //Give c1 an alias called c. c.x = 10; //Assign values to the data members of the structure variable through the reference c.y = 20; cout<< c1.x << c2.y; //The output result: 10 20 return 0; }

References to Pointer Types

References of pointer type are the most complex and difficult to understand part of references, and they also have distinctive features in syntax form.

Definition:

Type *&pointerReferenceName = pointer;

Look at an example:

Example

#include <iostream> using namespace std; int main(void) { int a = 10; //Define an integer variable a, with the value of a being 10. int *p = &a; //Define a pointer pointing to variable a int *&q = p; //Define a reference to a pointer, i.e., q is an alias of p. Definition method: Type *&pointer_reference_name = pointer; *q = 20; //Assign 20 to *q, which is equivalent to assigning 20 to *p, and thus equivalent to assigning 20 to a. cout<< a << endl; //The output value of a is 20. return 0; }

References as Function Parameters

Before learning references, when we studied C language, we learned such a classic program:

//Swap the two values void fun( int *a,int *b) //The formal parameters are two integer pointer variables { int temp = 0; //Define a temporary variable. A good habit is to define a variable and initialize it; c = *a; //Assign *a to c; *a = *b; //Assign *b to *a; *b = c; //Then assign c to *b; this completes the exchange of the values of a and b } int x = 10,y = 20; fun(&x,&y); //When calling in the main function, the actual arguments passed need to be written as &a, &b, which is troublesome and not easy to understand.

Now that we have learned references, what should it look like to achieve the same functionality?

Example

void fun (int &a, int &b) //The formal parameters are two integer references { int temp = 0; temp = a; a = b; b = temp; } int x = 10,y = 20; fun(x,y); //When called in the main function, after the actual arguments are passed, a is an alias of x, and b is an alias of y.

Original address: https://blog.csdn.net/stop_and_go/article/details/51334137