The first week of learning: references

concept
  1. The definition reference must be initialized to a variable
  2. Once the reference is bound, no other variables will be referenced
  3. References can only be variables, not constants or expressions
 	double a=4,b=5;
	double& r1=a;
	double& r2=r1;
	r2=10;
	cout<<a<<endl;
	r1=b;
	cout<<a<<endl;

Output result10 5

application

Exchange the value of two data swap()

	void swap1(int r1,int r2);
	void swap2(int& r1,int& r2);
	void swap3(int* r1,int* r2);

	int r1=10,r2=50;
	swap1(r1,r2);
	cout<<"r1: "<<r1<<"\tr2: "<<r2<<endl;
	swap2(r1,r2);
	cout<<"r1: "<<r1<<"\tr2: "<<r2<<endl;
	swap3(&r1,&r2);
	cout<<"r1: "<<r1<<"\tr2: "<<r2<<endl;

Output result

r1: 10  r2: 50
r1: 50  r2: 10
r1: 10  r2: 50

As the return value of the function

Often quoted

const int& r = n
Can't modify the content of the quotes through frequent quotes

const T&Constant variables of the type and type references cannot be used to initialize T &type references, because if you can, you can modify const type references.

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108545791