【C++】C++ 类中的 this 指针用法 ② ( 常量成员函数 | const 修饰成员函数分析 )





一、常量成员函数




1、const 修饰成员函数分析


在 C++ 类中 , 普通的非静态成员函数 , 可以使用 const 进行修饰 ,

在 下面的 Student 类中 , 定义了 void fun(int age, int height) 成员函数 , 下面使用 const 关键字修饰该类 ;


使用 const 修饰 成员函数 , 写法如下 , 在 fun() 之后使用 const 关键字修饰 函数 :

void fun(int age, int height) const

const 修饰的是 fun 函数的 第一个参数 Student* pThis 指针指向的内存空间 ;


C++ 编译器会将

void fun(int age, int height)

函数转为对应的 C 语言函数

Student_fun(Student* pThis, int age, int height)

使用 const 修饰函数 , 其本质是修饰 第一个参数 Student* pThis 指针指向的内存空间 和 指针本身 ;

void fun(int age, int height) const 

转换为 C 语言代码为 :

void Student_fun(const Student* const pThis, int age, int height) 

左数右指 , const 在 * 左边修饰的是内存中的数据, const 在 * 右边修饰的是指针本身 ;


代码示例 :

class Student
{
   
public:
	// 使用 const 修饰 类的成员函数 
	// const 关键字可以
	//		在 void fun(int age, int height) 之后 , 大括号之前 , 
	//		void fun(int age, int height) const
	// 
	// const 修饰的是 fun 函数的 第一个参数 Student* pThis 指针指向的内存空间 和 指针本身
	// 
	// C++ 编译器会将该函数转为 Student_fun(Student* pThis, int age, int height)
	// 使用 const 修饰函数 , 其本质是修饰 第一个参数 Student* pThis 指针指向的内存空间
	// void Student_fun(const Student* pThis, int age, int height) 
	//		左数右指 , const 在 * 左边修饰的是内存中的数据, const 在 * 右边修饰的是指针本身
	void fun(int age, int height) const
	{
   
		//this->age = age;
		//this->height = height;
	}

public:
	int age;		// 年龄
	int height;		// 身高
};

2、常量成员函数


使用 const 关键字 修饰成员函数 , 会将 成员函数 转化为 " 常量成员函数 " ;


" 常量成员函数 " 中 操作限制 :

  • 不能修改成员变量 : 不能修改 任何 成员变量 值 , 静态成员变量 与 非静态普通成员变量 都不能修改 ;
  • 不能调用非常量成员函数 : 只能调用 " 常量成员函数 " , 不能调用 非常量成员函数 , 以保证不会修改 成员变量 ;

" 常量成员函数 " 只能访问

  • 常量成员变量
  • 其它常量成员函数

如果类的 成员变量 不是 常量 , 那么 " 常量成员函数 " 不能访问它们 ;

public:
	int age;		// 年龄
	int height;		// 身高

如果类的 成员变量 是 常量 , 那么 " 常量成员函数 " 可以访问它们 , 注意 : 只能访问 , 不能修改 ;

public:
	const int age;		// 年龄
	const int height;	// 身高

如果 成员函数 被 const 关键字 声明为 常量成员函数 , 则在该函数中 不能修改 类对象中的 任何成员变量 ;

class Student
{
   
public:
	void fun(int age, int height) const
	{
   
		//this->age = age;
		//this->height = height;
	}

public:
	int age;		// 年龄
	int height;		// 身高
};

3、错误代码示例 - 常量成员函数修改成员变量


错误代码示例 :

class Student
{
   
public:
	// 带参构造函数
	Student(int age, int height)
	{
   
		this->age = age;
		this->height = height;
		cout << "执行 Student 的构造函数" << endl;
	}

	~Student()
	{
   
		cout << "执行 Student 的析构函数" << endl;
	}

	// 使用 const 修饰 类的成员函数 
	// const 关键字可以
	//		在 void fun(int age, int height) 之后 , 大括号之前 , 
	//		void fun(int age, int height) const
	// 
	// const 修饰的是 fun 函数的 第一个参数 Student* pThis 指针指向的内存空间 和 指针本身
	// 
	// C++ 编译器会将该函数转为 Student_fun(Student* pThis, int age, int height)
	// 使用 const 修饰函数 , 其本质是修饰 第一个参数 Student* pThis 指针指向的内存空间
	// void Student_fun(const Student* pThis, int age, int height) 
	//		左数右指 , const 在 * 左边修饰的是内存中的数据, const 在 * 右边修饰的是指针本身
	void fun(int age, int height) const
	{
   
		this->age = age;
		this->height = height;
	}

public:
	int age;		// 年龄
	int height;		// 身高
};

执行结果 :

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(33,7): error C3490: 由于正在通过常量对象访问“age”,因此无法对其进行修改
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(34,7): error C3490: 由于正在通过常量对象访问“height”,因此无法对其进行修改
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

在这里插入图片描述





二、完整代码示例



代码示例 :

#include "iostream"
using namespace std;

class Student
{
   
public:
	// 带参构造函数
	Student(int age, int height)
	{
   
		this->age = age;
		this->height = height;
		cout << "执行 Student 的构造函数" << endl;
	}

	~Student()
	{
   
		cout << "执行 Student 的析构函数" << endl;
	}

	// 使用 const 修饰 类的成员函数 
	// const 关键字可以
	//		在 void fun(int age, int height) 之后 , 大括号之前 , 
	//		void fun(int age, int height) const
	// 
	// const 修饰的是 fun 函数的 第一个参数 Student* pThis 指针指向的内存空间 和 指针本身
	// 
	// C++ 编译器会将该函数转为 Student_fun(Student* pThis, int age, int height)
	// 使用 const 修饰函数 , 其本质是修饰 第一个参数 Student* pThis 指针指向的内存空间
	// void Student_fun(const Student* const pThis, int age, int height) 
	//		左数右指 , const 在 * 左边修饰的是内存中的数据, const 在 * 右边修饰的是指针本身
	void fun(int age, int height) const
	{
   
		// 常量成员函数 中不能修改成员变量值
		//this->age = age;
		//this->height = height;
	}

public:
	int age;		// 年龄
	int height;		// 身高
};

int main()
{
   
	Student s(18, 173);
	s.fun(19, 175);


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

执行结果 :

执行 Student 的构造函数
Press any key to continue . . .

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 17:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 17:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 17:36:02       82 阅读
  4. Python语言-面向对象

    2023-12-08 17:36:02       91 阅读

热门阅读

  1. Fiddler抓包测试

    2023-12-08 17:36:02       60 阅读
  2. Vue+ElementUI实现输入框日期框下拉框动态展示

    2023-12-08 17:36:02       60 阅读
  3. 常用的git版本控制有哪些工具或网站呢?

    2023-12-08 17:36:02       73 阅读
  4. Git 还原文件修改

    2023-12-08 17:36:02       59 阅读
  5. 求int型正整数在内存中存储时1的个数

    2023-12-08 17:36:02       49 阅读
  6. 程序员学习方法

    2023-12-08 17:36:02       57 阅读
  7. flask之文件上传

    2023-12-08 17:36:02       63 阅读
  8. JDK、JRE、JVM、SE、EE、ME的区别

    2023-12-08 17:36:02       46 阅读
  9. Requests库详解、详细使用、高级用法

    2023-12-08 17:36:02       51 阅读