Skip to content

Wiki

Quote

About 3 min read

Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.Reference

1.1.Basic usage of references

**Purpose:** Assign an alias to a variable

Syntax: 数据类型 &别名 = 原名

Example:

int main() {
    // 程序从 main 函数开始执行,下面的语句会按顺序运行。

	int a = 10;
	int &b = a;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	b = 100;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;


	// 返回 0 表示程序正常结束。
	return 0;
}

Operation result:

a = 10
b = 10
a = 100
b = 100

1.2.Citation Notes

  • A reference must be initialized.
  • After a reference is initialized, it cannot be changed.

Example:

int main() {

	int a = 10;
	int b = 20;
	//int &c; //错误,引用必须初始化
	int &c = a; //一旦初始化后,就不可以更改
	c = b; //这是赋值操作,不是更改引用

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;


	return 0;
}

Operation result:

a = 20
b = 20
c = 20

1.3.Reference as a function parameter

**Purpose:** When passing function arguments, reference techniques can be used to allow the formal parameters to modify the actual parameters.

**Advantage:** It can simplify modifying the actual argument via a pointer.

Example:

//1. 值传递
void mySwap01(int a, int b) {
	int temp = a;
	a = b;
	b = temp;
}

//2. 地址传递
void mySwap02(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

//3. 引用传递
void mySwap03(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

int main() {

	int a = 10;
	int b = 20;

	mySwap01(a, b);
	cout << "a:" << a << " b:" << b << endl;

	mySwap02(&a, &b);
	cout << "a:" << a << " b:" << b << endl;

	mySwap03(a, b);
	cout << "a:" << a << " b:" << b << endl;


	return 0;
}

Operation result:

a:10 b:20
a:20 b:10
a:10 b:20

Summary: The effect produced by passing a reference parameter is the same as passing by address. The syntax of references is clearer and simpler.

1.4.Using references as function return values

Purpose: A reference can exist as the return value of a function.

Note:Do not return a reference to a local variable

Usage: function call as an lvalue

Example:

//返回局部变量引用
int& test01() {
	int a = 10; //局部变量
	return a;
}

//返回静态变量引用
int& test02() {
	static int a = 20;
	return a;
}

int main() {

	//不能返回局部变量的引用
	int& ref = test01();
	cout << "ref = " << ref << endl;
	cout << "ref = " << ref << endl;

	//如果函数做左值,那么必须返回引用
	int& ref2 = test02();
	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	test02() = 1000;

	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;


	return 0;
}

Run result: The program triggered undefined behavior; this run received SIGSEGV and then terminated abnormally, with no reliable normal output.

​

1.5.The Essence of Reference

本质:引用的本质在c++内部实现是一个指针常量.

Explanation example:

//发现是引用,转换为 int* const ref = &a;
void func(int& ref){
	ref = 100; // ref是引用,转换为*ref = 100
}
int main(){
	int a = 10;
    
    //自动转换为 int* const ref = &a; 指针常量是指针指向不可改,也说明为什么引用不可更改
	int& ref = a; 
	ref = 20; //内部发现ref是引用,自动帮我们转换为: *ref = 20;
    
	cout << "a:" << a << endl;
	cout << "ref:" << ref << endl;
    
	func(a);
	return 0;
}

Operation result:

a:20
ref:20

Conclusion: C++ It is recommended to use reference techniques because the syntax is convenient; a reference is essentially a pointer constant, but the compiler handles all pointer operations for us.

1.6.constant reference

**Purpose:** Constant references are mainly used to modify formal parameters to prevent accidental modification.

In a function parameter list, ==const can be added to qualify a parameter== to prevent the parameter from modifying the argument.

Example:

//引用使用的场景,通常用来修饰形参
void showValue(const int& v) {
	//v += 10;
	cout << v << endl;
}

int main() {

	//int& ref = 10;  引用本身需要一个合法的内存空间,因此这行错误
	//加入const就可以了,编译器优化代码,int temp = 10; const int& ref = temp;
	const int& ref = 10;

	//ref = 100;  //加入const后不可以修改变量
	cout << ref << endl;

	//函数中利用常量引用防止误操作修改实参
	int a = 10;
	showValue(a);


	return 0;
}

Operation result:

10
10