【C++】C++ 类中的 this 指针用法 ② ( 常量成员函数 | const 修饰成员函数分析 )
  K1I6GvxBEuyy 2023年11月02日 108 0



文章目录

  • 一、常量成员函数
  • 1、const 修饰成员函数分析
  • 2、常量成员函数
  • 3、错误代码示例 - 常量成员函数修改成员变量
  • 二、完整代码示例







一、常量成员函数




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 个 ==========

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






二、完整代码示例



代码示例 :

#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 . . .

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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  8Tw5Riv1mGFK   2024年05月01日   80   0   0 C++
  BYaHC1OPAeY4   2024年05月08日   56   0   0 C++
  yZdUbUDB8h5t   2024年05月05日   43   0   0 C++
  oXKBKZoQY2lx   2024年05月17日   57   0   0 C++
K1I6GvxBEuyy
作者其他文章 更多