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


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

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

相关推荐
csbysj20202 小时前
jQuery 删除元素
开发语言
xxy-mm2 小时前
Javascript 中的继承
开发语言·javascript·ecmascript
quikai19815 小时前
python练习第二组
开发语言·python
AI视觉网奇5 小时前
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr
开发语言·c++·算法
智者知已应修善业5 小时前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
wjs20245 小时前
并查集快速合并
开发语言
free-elcmacom5 小时前
MATLAB与高等数学<1>一道曲面积分题的几何直观
开发语言·数学建模·matlab·高等数学
LaoZhangGong1235 小时前
深度学习uip中的“psock.c和psock.h”
c语言·开发语言
Tony Bai5 小时前
Go 安全新提案:runtime/secret 能否终结密钥残留的噩梦?
java·开发语言·jvm·安全·golang
oioihoii5 小时前
C++11到C++23语法糖万字详解
java·c++·c++23