【C++第二阶段】赋值运算符重载

你好你好!

以下内容仅为当前认识,可能有不足之处,欢迎讨论!


文章目录


赋值运算符重载

实验①,还没有对析构运算符重载时

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;

class Person {
	friend void test_0210_0();
public:
	Person();
	Person(int age);
private:
	int *person_age;
};

Person::Person(int age) {
	person_age = new int(age);//构造函数,使得传入的参数能够作为类的成员属性传进去
}

void test_0210_0() {
	Person pe(18);
	Person rs(20);
	cout << "没有重载赋值运算符之前,实验部分.........." << endl;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << "---------------------------------" << endl;
	pe = rs;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << endl;
}

int main() {
	cout << "hello world !" << endl;
	test_0210_0();
    system("pause");
    return 0;
}

结果:

重载析构函数后,没有重载赋值运算符,会出现释放内存后,另一个对象的指针指向这个不存在的地址的问题。

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;

class Person {
	friend void test_0210_0();
	friend void test_0210_1();
	friend void test_0210_2();
	friend void test_0210_3();
public:
	Person();
	~Person();
	Person(int age);
private:
	int *person_age;
};

Person::Person(int age) {
	person_age = new int(age);//构造函数,使得传入的参数能够作为类的成员属性传进去
}

Person::~Person() {
	//重载析构函数,确定有这个问题:浅拷贝如果在这里人为释放地址,会使得后面的新对象发生错误
	if (person_age != NULL) {
		cout << "person_age指针有地址,删除中.........." << endl;
		delete person_age;
		person_age = NULL;
		cout << "删除成功!" << endl;
	}
}


void test_0210_0() {
	Person pe(18);
	Person rs(20);
	cout << "没有重载赋值运算符之前,实验部分.........." << endl;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << "---------------------------------" << endl;
	pe = rs;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << endl;
}

int main() {
	cout << "hello world !" << endl;
	test_0210_0();
    system("pause");
    return 0;
}

结果:

运行到这一步就停住...

对同一块地址重复释放了。

所以,需要重写赋值运算符内容。

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;

class Person {
	friend void test_0210_0();
	friend void test_0210_1();
public:
	Person();
	~Person();
	Person(int age,string name);
	//返回值写什么?写类
	Person& operator=(Person &person); 
private:
	int *person_age;
	string person_name;
};

Person::Person(int age , string name) {
	person_age = new int(age);//构造函数,使得传入的参数能够作为类的成员属性传进去
	string person_name = name;
}

Person::~Person() {
	//重载析构函数,确定有这个问题:浅拷贝如果在这里人为释放地址,会使得后面的新对象发生错误
	if (person_age != NULL) {
		cout << "person_age指针有内容,释放中.........." << endl;
		delete person_age;
		person_age = NULL;
		cout << "删除成功!" << endl;
	}
	else {
		
	}
}

Person& Person::operator=(Person& temp) {//参数这里应该用引用,因为不需要重复赋值
	//这里的参数是后面的rs
	//if (person_age != NULL) {//应该先判断属性中的person_age 是否有地址,如果没有,先删除了再说

	if (this != NULL) {
		delete person_age;

		person_age = new int(*temp.person_age);//整型指针指向新的内存空间开辟的相同值
	}
	return *this;//这里返回的是具体的对象,如果函数定义是person,相当于新的值,而不是本身。
}

void test_0210_0() {
	string name_pe = "pe";
	string name_rs = "rs";
	Person pe(18 , name_pe);
	Person rs(20 , name_rs);
	cout << "没有重载赋值运算符之前,实验部分.........." << endl;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << "---------------------------------" << endl;
	pe = rs;
	//rs = pe;
	cout << "重载赋值运算符之后,实验部分..........." << endl;
	cout << "pe age = " << *pe.person_age << "." << endl;
	cout << "rs age = " << *rs.person_age << "." << endl;
	cout << endl;
}

int main() {
	cout << "hello world !" << endl;
	test_0210_0();
	system("pause");
    return 0;
}

同时,写了返回值后,能够链式调用赋值运算符。

遗留有一个问题:为什么这里

cpp 复制代码
Person& Person::operator=(Person& temp) {//参数这里应该用引用,因为不需要重复赋值
	//这里的参数是后面的rs
	//if (person_age != NULL) {//应该先判断属性中的person_age 是否有地址,如果没有,先删除了再说

	if (this != NULL) {
		delete person_age;

		person_age = new int(*temp.person_age);//整型指针指向新的内存空间开辟的相同值
	}
	return *this;//这里返回的是具体的对象,如果函数定义是person,相当于新的值,而不是本身。
}

返回值为Person,没有报错(因为引用和Person类一样),但是使用完后调用这个赋值运算符的对象会调用析构函数呢?


以上是我的学习笔记,希望对你有所帮助!

如有不当之处欢迎指出!谢谢!

相关推荐
煸橙干儿~~几秒前
分析JS Crash(进程崩溃)
java·前端·javascript
2401_854391081 分钟前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
Amor风信子2 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
我是陈泽5 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
Mr.Z.41112 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
优雅的小武先生16 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death20016 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
虽千万人 吾往矣22 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人25 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈27 分钟前
C++——list
开发语言·c++·list