[C++] Usage of this pointer in C++ classes ③ (mutual conversion between global functions and member functions | parameterized constructors to set default parameter values | return anonymous objects and return references)





1. Mutual conversion between global functions and member functions




1. Convert member function to global function - one more parameter


The C++ compiler will convert the member functions of the C++ class into global functions during the compilation phase . During the conversion, it will add a parameter to the beginning of the parameter list. This added parameter is a pointer to the object itself;


In the Student class, the following functions are defined:

	// 成员函数 转为 全局函数 , 多了一个参数 Student* pThis 作为第一个参数
	void print()
	{
    
    
		cout << "age = " << this->age << " , height = " << this->height << endl;
	}

Convert the print member function in the above Student class into a global function. The converted code is:

// 将成员函数 void print() 
// 转为 全局函数
void Student_print(Student* pThis)
{
    
    
	cout << "age = " << pThis->age << " , height = " << pThis->height << endl;
}

After the conversion is completed, a parameter Student* pThis is added to the parameter list at the beginning of the list;

For detailed code, refer to the complete code example at the end;


2. Convert global function to member function - hide operands through this pointer


When a global function is converted into a member function, a parameter needs to be hidden, that is, the left operand is hidden through the this pointer. The object itself is the left operand. In the member function, the members of the object itself are accessed through the this pointer;


In the global function, implement the addition of two Student classes and receive two parameters of Student reference type. The reference is equivalent to a first-level pointer;

// 全局函数中 , 将两个 Student 对象相加
// 引用的 等同于 一级指针 , Student 引用用法与 Student 对象用法相同
// 全局函数 转为 成员函数 , 少了一个参数
Student StudentPlus(Student& s1, Student& s2)
{
    
    
	Student s;
	s.age = s1.age + s2.age;
	s.height = s1.height + s2.height;
	return s;
}

After converting to a member function, the member function has one less parameter than the global function, and the missing parameter is the object itself;

	// 成员函数中, 将两个 Student 对象相加
	// 全局函数 转为 成员函数 , 少了一个参数
	Student StudentPlus(Student& s2)
	{
    
    
		Student s;
		s.age = this->age + s2.age;
		s.height = this->age + s2.height;
		// 注意 : 返回的是一个匿名对象
		return s;
	}

For detailed code, refer to the complete code example at the end;





2. Parametric constructor sets default parameter values



If a parameterized constructor is defined for the Student class, its default parameterless constructor will not be generated;

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

At this time, if you want to create a Student object, you can only call the above parameterized constructor. If you use the Student s2 method to call the default constructor to create a Student object, an error will be reported;

Insert image description here


Take the following constructor with parameters and set a default value for the parameters of its parameterized constructor. At this time, you can use 类名 对象名the method to define object variables;

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

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

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

At this point, you can use the default constructor to create a Student object;

Insert image description here





3. Returning anonymous objects and returning references



In the above chapter, two Student objects are added and an anonymous object is returned. The anonymous object is a newly created object in the member function;

	// 成员函数中, 将两个 Student 对象相加
	// 全局函数 转为 成员函数 , 少了一个参数
	// 返回一个新 Student 对象
	Student StudentPlus(Student& s2)
	{
    
    
		Student s;
		s.age = this->age + s2.age;
		s.height = this->height + s2.height;
		// 注意 : 返回的是一个匿名对象
		return s;
	}

If a new object is not returned, but the two objects are added, and the final result is accumulated into this object, then the Student reference can be returned;

	// 成员函数中, 将两个 Student 对象相加
	// 全局函数 转为 成员函数 , 少了一个参数
	// 两个 对象相加 , 最终结果累加到 本对象中
	Student& StudentPlus2(Student& s2)
	{
    
    
		this->age = this->age + s2.age;
		this->height = this->height + s2.height;
		// 注意 : 返回的是一个引用 , 就是返回自身对象变量
		// this 是指针 , *this 是指针指向的 自身对象
		return *this;
	}

Returning a reference means returning the own object;

this is a pointer, *this is the actual data of the own object pointed to by the pointer,

Returning a reference means returning the actual data of the own object *this;





4. Complete code example



Code example:

#include "iostream"
using namespace std;

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

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

	// 成员函数中, 将两个 Student 对象相加
	// 全局函数 转为 成员函数 , 少了一个参数
	// 返回一个新 Student 对象
	Student StudentPlus(Student& s2)
	{
    
    
		Student s;
		s.age = this->age + s2.age;
		s.height = this->height + s2.height;
		// 注意 : 返回的是一个匿名对象
		return s;
	}

	// 成员函数中, 将两个 Student 对象相加
	// 全局函数 转为 成员函数 , 少了一个参数
	// 两个 对象相加 , 最终结果累加到 本对象中
	// 注意此处 : 函数重载 不以 返回值为标准 , 函数名需要修改
	Student& StudentPlus2(Student& s2)
	{
    
    
		this->age = this->age + s2.age;
		this->height = this->height + s2.height;
		// 注意 : 返回的是一个引用 , 就是返回自身对象变量
		// this 是指针 , *this 是指针指向的 自身对象
		return *this;
	}

	// 成员函数 转为 全局函数 , 多了一个参数 Student* pThis 作为第一个参数
	void print()
	{
    
    
		cout << "age = " << this->age << " , height = " << this->height << endl;
	}

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

// 将成员函数 void print() 
// 转为 全局函数
void Student_print(Student* pThis)
{
    
    
	cout << "age = " << pThis->age << " , height = " << pThis->height << endl;
}


// 全局函数中 , 将两个 Student 对象相加
// 引用的 等同于 一级指针 , Student 引用用法与 Student 对象用法相同
// 全局函数 转为 成员函数 , 少了一个参数
Student StudentPlus(Student& s1, Student& s2)
{
    
    
	Student s;
	s.age = s1.age + s2.age;
	s.height = s1.height + s2.height;
	return s;
}


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

	// 调用有参构造函数 , 有参构造函数参数使用默认值
	Student s2;
	s2.print();

	// 调用全局函数, 将两个 Student 对象相加
	// 函数返回的 匿名对象 用于 s3 初始化 
	// 直接将匿名对象转为普通对象
	Student s3 = StudentPlus(s, s2);
	s3.print();

	// 调用成员函数, 将两个 Student 对象相加
	Student s4;
	// 函数返回的 匿名对象 用于 s4 赋值
	// 此处匿名对象 为 s4 赋值 , 赋值完毕后 匿名对象销毁
	s4 = s.StudentPlus(s2);
	s3.print();
	

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

    return 0;
}

Results of the :

执行 Student 的构造函数
age = 18 , height = 180
执行 Student 的构造函数
age = 1 , height = 1
执行 Student 的构造函数
执行 Student 的析构函数
age = 19 , height = 181
执行 Student 的构造函数
执行 Student 的构造函数
执行 Student 的析构函数
执行 Student 的析构函数
age = 19 , height = 181
请按任意键继续. . .

Insert image description here

Guess you like

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