【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类一样),但是使用完后调用这个赋值运算符的对象会调用析构函数呢?


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

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

相关推荐
D_evil__5 小时前
【Effective Modern C++】第三章 转向现代C++:16. 让const成员函数线程安全
c++
微风中的麦穗5 小时前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_949146536 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术6 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学6 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
毕设源码-朱学姐6 小时前
【开题答辩全过程】以 基于JavaWeb的网上家具商城设计与实现为例,包含答辩的问题和答案
java
Queenie_Charlie7 小时前
前缀和的前缀和
数据结构·c++·树状数组
mftang7 小时前
Python 字符串拼接成字节详解
开发语言·python
jasligea8 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka8 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏