[C++] Usage of this pointer in C++ classes ② (constant member function | analysis of const modified member function)





1. Constant member functions




1. Analysis of const modified member functions


In a C++ class, ordinary non-static member functions can be modified with const.

In the Student class below, the void fun(int age, int height) member function is defined. The const keyword is used to modify the class below;


Use const to modify member functions. The writing method is as follows. Use the const keyword after fun() to modify the function:

void fun(int age, int height) const

const modifies the memory space pointed by the Student* pThis pointer of the first parameter of the fun function;


The C++ compiler will

void fun(int age, int height)

The function is converted into the corresponding C language function

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

The essence of using const to modify a function is to modify the memory space pointed by the first parameter Student* pThis pointer and the pointer itself;

Will

void fun(int age, int height) const 

Converted to C language code:

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

The number on the left points to the right, const on the left of * modifies the data in the memory, const on the right of * modifies the pointer itself;


Code example:

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. Constant member functions


Using the const keyword to modify a member function will convert the member function into a "constant member function";


Operation restrictions in "constant member functions":

  • Member variables cannot be modified: any member variable value cannot be modified, neither static member variables nor non-static ordinary member variables can be modified;
  • Non-constant member functions cannot be called: only "constant member functions" can be called, and non-constant member functions cannot be called to ensure that member variables will not be modified;

"Constant member functions" can only access

  • constant member variables
  • Other constant member functions

If the member variables of the class are not constant, then "constant member functions" cannot access them;

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

If the member variables of the class are constants, then "constant member functions" can access them. Note: they can only be accessed, not modified;

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

If a member function is declared as a constant member function with the const keyword, any member variable in the class object cannot be modified in the function;

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

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

3. Error code example - constant member function modifies member variables


Error code example:

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;		// 身高
};

Results of the :

已启动生成…
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==========

Insert image description here





2. Complete code example



Code example:

#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;
}

Results of the :

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

Insert image description here

Guess you like

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