Article Directory
1. Common references
1. Concept description
In the previous [C++] Detailed Explanation of C++ References ① ~ ⑦ blogs, all the explanations are ordinary references, that is, assigning ordinary variables to references. The process is as follows: first define the ordinary variable a, and then define the reference b of the existing variable a;
The reference b here is a normal reference;
// 定义变量 a
int a = 10;
// 定义变量 a 的引用 b
int& b = a;
2. Code example - common reference
Common reference code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
int main()
{
// I . 普通引用
// 定义变量 a
int a = 10;
// 定义变量 a 的 普通引用 b
int& b = a;
// 打印 普通引用 b 的值
printf("b = %d\n", b);
// 修改普通引用 b 的值
b = 20;
// 打印 普通引用 b 的值
printf("b = %d\n", b);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
b = 10
b = 20
Press any key to continue . . .
2. Constant reference
1. The introduction of constant references
The opposite of a variable reference is a constant reference;
- By ordinary reference, the value of the variable can be modified;
- Constant references are not modifiable. Declare a constant reference of an ordinary variable, and then no longer use the variable, but use the constant reference instead, which is equivalent to converting the variable into a constant;
Constant references can convert variable references into constants; that is, variables cannot be modified through constant references;
If you modify the value referenced by a constant, an error will be reported at compile time:
error C3892: “b”: 不能给常量赋值
2. Constant reference concept and syntax
In the C++ language, "constant reference", the English name is Const Reference, is a type of reference, and the other kind of reference is "common reference";
Use "constant reference" to pass the value of a variable to the constant reference formal parameter of a function, which can ensure that the variable value will not be modified inside the function;
"Constant reference" is often used in the following scenarios:
- as a function parameter
- as function return value
- define object members
Constant reference syntax:
const T& variable_name = value;
- T is the type name of the constant reference , such as: int , string ;
- variable_name is the name of the constant reference , calling the constant reference, the value of the value variable cannot be modified;
- value is the referenced variable, calling this variable can modify the value of the variable;
2. Code example - constant reference cannot be modified
In the following function, the ordinary variable a is declared first,
Then define the constant reference b of the variable a,
Constant reference b is essentially a constant, and the value of variable a can be obtained through constant reference b, but the value of variable a cannot be modified;
If you forcibly modify the value of variable a through constant reference b, an error will be reported:
error C3892: “b”: 不能给常量赋值
Error code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
int main()
{
// II . 常量引用
// 定义变量 a
int a = 10;
// 定义变量 a 的 常量引用 b
const int& b = a;
// 打印 常量引用 b 的值
printf("b = %d\n", b);
// 修改 常量引用 b 的值
// 报错 : error C3892: “b”: 不能给常量赋值
b = 20;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(24,2): error C3892: “b”: 不能给常量赋值
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========