[C++] C++ reference detailed explanation ⑩ (constant reference case)


In the C++ language, a constant reference is a type of reference;

With constant reference, you can pass a variable reference as an actual parameter to a function parameter, and at the same time ensure that the value will not be modified inside the function; this not only ensures the efficiency of parameter transfer, but also ensures data security;

Important usage scenarios for constant references:

  • function parameter passing
  • function return value
  • object member

This blog will give several cases of constant references;





1. Constant reference syntax




1. Introduction to Grammar


Using "ordinary variables" to initialize "constant references" is to assign ordinary variables to constant applications, and it can also be understood as converting variables to constants;


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. Common reference syntax example


Example of constant reference: The following code is the above-mentioned initialization of constant reference using ordinary variables:

	// 定义变量 a
	int a = 10;

	// 定义变量 a 的 常量引用 b
	const int& b = a;




2. Constant reference syntax




1. Example of int type constant reference


If the following function is defined, the parameter type is a constant reference of type const int&,

// 常量引用作为函数参数  
void fun(const int& num) {
    
    
	// 在函数中不能修改上述 num 值 
	printf("fun 函数接收到参数 num = %d\n", num);

	// 如果尝试修改 常量引用 num 值 , 就会在编译时报错
	num = 0;
}

If you try to modify the constant reference num value, an error will be reported during compilation, and the error message is as follows:

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(17,2): error C3892: “num”: 不能给常量赋值
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

insert image description here


Correct code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 导入 C 头文件
#include <stdio.h>

// 常量引用作为函数参数  
void fun(const int& num) {
    
    
	// 在函数中不能修改上述 num 值 
	printf("fun 函数接收到参数 num = %d\n", num);

	// 如果尝试修改 常量引用 num 值 , 就会在编译时报错
	//num = 0;
}

int main()
{
    
    
	// 定义普通变量 a
	int a = 10;

	// 向函数中传入 变量 a 作为常量引用参数 
	// 相当于将 &a 地址传入
	fun(a);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Results of the :

fun 函数接收到参数 num = 10
Press any key to continue . . .

insert image description here


2. Example of structure type constant reference


Define a function that receives a constant reference of the structure class type, then the object member pointed to by the structure constant reference cannot be modified;

The defined structure class is as follows:

// 定义一个结构体类型, 之后使用该类型的常量引用进行测试
struct Student
{
    
    
	char name[64];
	int age;
};

The function receives a constant reference of the structure type as a parameter, and the members of this parameter cannot be modified. If you try to modify the value of the constant reference student member, an error will be reported during compilation;

// 常量引用作为函数参数  
void fun(const Student& student) {
    
    
	// 在函数中不能修改上述 num 值 
	printf("fun 函数接收到参数 student = %d\n", student.age);

	// 如果尝试修改 常量引用 student 成员的值 , 就会在编译时报错
	//student.age = 0;
}

insert image description here


Correct code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 导入 C 头文件
#include <stdio.h>

// 定义一个结构体类型, 之后使用该类型的常量引用进行测试
struct Student
{
    
    
	char name[64];
	int age;
};

// 常量引用作为函数参数  
void fun(const Student& student) {
    
    
	// 在函数中不能修改上述 num 值 
	printf("fun 函数接收到参数 student = %d\n", student.age);

	// 如果尝试修改 常量引用 student 成员的值 , 就会在编译时报错
	//student.age = 0;
}

int main()
{
    
    
	// 定义 Student 类对象
	Student s;
	// 设置 Student 对象成员值
	s.age = 18;

	// 向函数中传入 Student 对象 s 作为 Student 类型的常量引用参数 
	// 相当于将 &s 地址传入
	fun(s);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Results of the :

fun 函数接收到参数 student = 18
Press any key to continue . . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132546843