C++中return this与*this这两种不同的返回值形式

本文讨论的是在C++的类相关知识中,关于函数的返回值为 *thisthis 的情况。

return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 )。return this返回当前对象的地址(指向当前对象的指针), 下面我们来看看程序吧:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class A
{
public:
	int x;
	A* get()
	{
		return this;
	}
};

int main()
{
	A a;
	a.x = 4;

	if (&a == a.get())
	{
		cout << "地址" << endl;
	}
	else
	{
		cout << "非地址" << endl;
	}

	return 0;
}

控制台显示:地址
这说明get()方法中的return this,返回的是当前对象的地址

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class A
{
public:
	int x;
	A() {
		x = 0;
	}
    //这里写了一个拷贝构造函数,所以得再提供一个构造函数
	A(const A &a) {
		x = a.x;
		printf("copy constructor is called\n");
	}
	
	//* 如果显示的写了一个普通的构造函数,会隐藏默认的无参构造函数
	//*如果显示的写了一个拷贝构造函数,会隐藏默认的无参构造函数和默认构造函数
	//* 如果显示的写了一个析构函数,会隐藏默认的析构函数
	A get()
	{
		return *this; //返回当前对象的拷贝
	}

};

int main()
{
	A a;
	a.x = 4;

	if (a.x == a.get().x)
	{
		cout << a.x << endl;
	}
	else
	{
		cout << "no" << endl;
	}

	if (&a == &a.get())
	{
		cout << "地址" << endl;
	}
	else
	{
		cout << "不是地址" << endl;
	}

	return 0;
}

控制台显示:

copy constructor is called
4
copy constructor is called
不是地址

这说明get()方法中的return *this在程序运行过程中返回的不是地址,而是当前对象的一个拷贝(因为调用了拷贝构造函数)

如果将程序返回值改为A&,代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class A
{
public:
	int x;
	A() {
		x = 0;
	}
    //这里写了一个拷贝构造函数,所以得再提供一个构造函数
	A(const A &a) {
		x = a.x;
		printf("copy constructor is called\n");
	}
	
	//* 如果显示的写了一个普通的构造函数,会隐藏默认的无参构造函数
	//*如果显示的写了一个拷贝构造函数,会隐藏默认的无参构造函数和默认构造函数
	//* 如果显示的写了一个析构函数,会隐藏默认的析构函数
	A& get()
	{
		return *this; //返回当前对象的拷贝
	}

};

int main()
{
	A a;
	a.x = 4;

	if (a.x == a.get().x)
	{
		cout << a.x << endl;
	}
	else
	{
		cout << "no" << endl;
	}

	if (&a == &a.get())
	{
		cout << "地址" << endl;
	}
	else
	{
		cout << "不是地址" << endl;
	}

	return 0;
}

控制台显示:

4
不是地址

说明当返回类型为 A& 时,没有调用拷贝构造函数,返回的是当前对象本身(即引用),而不是拷贝出来的副本。

  • 20
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
下面是一个简单的学生成绩管理系统的代码,包括了管理人员和学生两种身份登陆系统的功能。 ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 学生类 class Student { public: Student(int id, string name, int grade) : id(id), name(name), grade(grade) {} int getId() { return id; } string getName() { return name; } int getGrade() { return grade; } private: int id; // 学号 string name; // 姓名 int grade; // 成绩 }; // 管理员类 class Admin { public: Admin(string username, string password) : username(username), password(password) {} bool login(string username, string password) { return this->username == username && this->password == password; } void addStudent(int id, string name, int grade) { students.push_back(Student(id, name, grade)); cout << "添加成功!" << endl; } void showStudents() { cout << "学号\t姓名\t成绩" << endl; for (auto student : students) { cout << student.getId() << "\t" << student.getName() << "\t" << student.getGrade() << endl; } } private: string username; // 用户名 string password; // 密码 vector<Student> students; // 学生信息列表 }; int main() { Admin admin("admin", "123456"); // 管理员账号密码 while (true) { cout << "请输入用户名和密码:" << endl; string username, password; cin >> username >> password; if (admin.login(username, password)) { cout << "登陆成功!" << endl; while (true) { cout << "请选择操作:" << endl; cout << "1. 添加学生" << endl; cout << "2. 查看学生列表" << endl; cout << "3. 退出" << endl; int choice; cin >> choice; if (choice == 1) { int id, grade; string name; cout << "请输入学号、姓名和成绩:" << endl; cin >> id >> name >> grade; admin.addStudent(id, name, grade); } else if (choice == 2) { admin.showStudents(); } else if (choice == 3) { break; } else { cout << "无效的选择!" << endl; } } break; } else { cout << "用户名或密码错误,请重新输入!" << endl; } } return 0; } ``` 这个代码,我们定义了一个学生类和一个管理员类。学生类包括学号、姓名和成绩三个属性;管理员类包括用户名、密码和学生信息列表三个属性。管理员类提供了登陆、添加学生和查看学生列表三个方法。 在主函数,我们首先输入管理员的用户名和密码进行登陆,如果登陆成功,则进入管理员界面,可以进行添加学生和查看学生列表操作。如果用户名或密码错误,则需要重新输入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值