你好你好!
以下内容仅为当前认识,可能有不足之处,欢迎讨论!
赋值运算符重载
实验①,还没有对析构运算符重载时
#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;
}
结果:
重载析构函数后,没有重载赋值运算符,会出现释放内存后,另一个对象的指针指向这个不存在的地址的问题。
#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;
}
结果:
运行到这一步就停住…
对同一块地址重复释放了。
所以,需要重写赋值运算符内容。
#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;
}
同时,写了返回值后,能够链式调用赋值运算符。
遗留有一个问题:为什么这里
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类一样),但是使用完后调用这个赋值运算符的对象会调用析构函数呢?
以上是我的学习笔记,希望对你有所帮助!
如有不当之处欢迎指出!谢谢!