【C++】C++クラスでのこのポインタの使い方②(定数メンバ関数|const修飾メンバ関数の解析)





1. 定数メンバー関数




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

ここに画像の説明を挿入します





2. 完全なコード例



コード例:

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

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/han1202012/article/details/133255715