Base ---- C ++ base references

Base ---- C ++ base references

The concept of references: reference variable is an alias, that is to say, it is a name of another variable that already exists. Once the reference is initialized to a variable, you can use the reference name or the name of the variable to point to the variable. such as:

int n;
int &r=n;

Well, now, we have defined a reference. And initializes it to a variable. At this time, r n became an alias. You operate on r n is equivalent to the deity to operate. The two of them become one flesh.

See a chestnut:

int n=7;
int &r=n;
r=4;
cout<<n<<endl;//结果为4
cout<<r<<endl;//结果为4
n=5;
cout<<r<<endl;//结果为5

There are several places to note:

1. Define sure to initialize it as a reference when referring to a variable.
2. After initialization, he has been the amount of change in the application.

Reference applications:

void swap(int &a,int &b){
    int tmp;
    tmp=a;
    a=b;
    b=tmp;
}

Often quoted:

Often not by reference to modify the contents of their references.

Thank you for taking the time to listen to a word chicken dish! !

Guess you like

Origin www.cnblogs.com/tushukai/p/11273175.html