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

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


文章目录


赋值运算符重载

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

#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;
}

结果:

image-20240210214939218

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

#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;
}

结果:

运行到这一步就停住…

image-20240210224641432

image-20240210224833166

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

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

#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;
}

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

image-20240211002249777

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

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


以上是我的学习笔记,希望对你有所帮助!
如有不当之处欢迎指出!谢谢!

学吧,学无止境,太深了

最近更新

  1. TCP协议是安全的吗?

    2024-02-13 23:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-13 23:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-13 23:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-13 23:00:02       18 阅读

热门阅读

  1. 业务流程

    2024-02-13 23:00:02       33 阅读
  2. 安装PostgreSQL和PostGIS

    2024-02-13 23:00:02       24 阅读
  3. 所有设计模式大全及学习链接

    2024-02-13 23:00:02       40 阅读
  4. 一篇文章学透所有Python知识

    2024-02-13 23:00:02       32 阅读
  5. Python列表中的clear功能及用法举例

    2024-02-13 23:00:02       34 阅读
  6. 突破编程_C++_面试(基础知识(13))

    2024-02-13 23:00:02       26 阅读
  7. 读书笔记:《人件:PeopleWare》

    2024-02-13 23:00:02       32 阅读
  8. 阿里云Redis

    2024-02-13 23:00:02       29 阅读