[C++] Usage of this pointer in C++ classes ① (Introduction of this pointer in C++ classes | Usage of this pointer | Code examples)





1. This pointer in C++ class




1. Introduction of this pointer in C++ class


In a C++ class, the this pointer is a special pointer, which is automatically generated by the system and does not need to be manually declared and defined. This pointer can be called in every non-static member function in the class;

The this pointer is a pointer to the calling object itself, which is the memory address of the instance object that calls the member function;

Since this pointer can only be used inside non-static member functions, this pointer is a pointer used internally in the class. You can use this to access all public/protected/private members in the instance object;


2. Usage of this pointer in C++ classes


Usage of this pointer in C++ classes:

  • Use this as a pointer: In non-static member functions, use this directly as the pointer of this instance object;
this
  • Use this-> to access member variables: In a non-static member function, directly use the following syntax to access the non-static member variables in this instance object;
this->成员变量名
  • * Use ( this). To access member variables: In a non-static member function, directly use the following syntax to access the non-static member variables in this instance object; first obtain the data pointed to by the pointer and then access the member variables in the data;
(*this).成员变量名

In the C++ class, the member variables age and height are defined and initialized in the constructor. You can use this->age to access the age member variable and this->height to access the height member variable;

In the following code, the parameter names are also age and height, which have the same name as the member variable. Using this pointer can effectively distinguish the problem of the function parameter and member variable having the same name;

Use the code this->age = age; to assign a value to the age variable, this->age is a member variable, and age is a parameter of the function;


Sample code:

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

	void print() {
    
    
		// this 指针隐式传递
		std::cout << "age: " << age << std::endl;
		// 通过指针 访问成员变量
		std::cout << "this->age: " << this->age << std::endl;
		// 先获取指针指向的数据 然后访问数据中的成员变量
		std::cout << "(*this).age: " << (*this).age << std::endl;
	}

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

3. Complete code example


In the code below,

The parameters of the constructor Student(int age, int height) are named age and height,

The member variables are int age and int height,

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

It just so happens that the member variable name is the same as the parameter name. The this keyword is used here to solve the name conflict problem;


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

	void print() {
    
    
		// this 指针隐式传递
		std::cout << "age: " << age << std::endl;
		// 通过指针 访问成员变量
		std::cout << "this->age: " << this->age << std::endl;
		// 先获取指针指向的数据 然后访问数据中的成员变量
		std::cout << "(*this).age: " << (*this).age << std::endl;
	}

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

int main() {
    
    
	
	// 调用有参构造函数 创建 Student 实例对象
	Student s(18, 180);

	cout<< "s.age" << s.age << " , s.height" << s.height << endl;

	s.print();
	

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

    return 0;
}

Results of the :

执行 Student 的构造函数
s.age18 , s.height180
age: 18
this->age: 18
(*this).age: 18
请按任意键继续. . .

Insert image description here

Guess you like

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