C++ 类的赋值运算符''=''重载的方法实现

什么类需要重载赋值运算符

先来看一个普通类的直接赋值。

#include <iostream>

using namespace std;

class person{

int age;

public:

person(const int& a=10):age(a){} //构造函数

~person(); //析构函数

void showAdd(); //打印age的地址

};

person::~person(){cout<<"析构\n";}

void person::showAdd() {cout <<hex<< &age<<endl;}

int main() {

person a(11);

person b;

b = a;

a.showAdd();

b.showAdd();

return 0;

}

/*

结果是:

0x7fffffffdc5c

0x7fffffffdc60

析构

析构

*/

这是这个程序的内存情况,一切都运行的很正常,不需要运算符重载

看下边这个例子,这个类的构造函数需要申请(new)堆内存:

#include <iostream>

using namespace std;

class person{

int* age;

public:

person(const int& a=10); //构造函数

~person(); //析构函数

void showAdd(); //打印age的地址

void show(); //打印age指向的值

void set(const int& a){*age=a;}

};

person::person(const int& a) {age = new int(a);}

person::~person(){delete age; cout<<"析构\n";}

void person::showAdd() {cout << hex << age<<endl;}

void person::show() {cout<<*age<<endl;}

void f(person& a) {

person b;

b=a;

a.show();

b.show();

a.showAdd();

b.showAdd();

//因为b是局部变量,所以进入main函数之前,b会自动调用析构函数

}

int main() {

person a(11);

f(a);

cout<<"进入main函数\n";

a.set(9); //因为b已经释放过age指针,set应该会出错

a.show();

return 0;

}

运行结果如下:

这是这个程序进入 f() 函数时的内存情况,两个age指针指向同一块内存。

这是这个程序退出 f() 函数进入main函数的情况,因为b是局部变量,所以f()函数结束的时候,b会调用析构函数,释放age指向的堆内存。这时候a.set()就会发生错误,因为内存已经释放,无权修改内存里的值。就算没有set()函数,main函数结束的时候还会产生doublefree的错误,同一块内存被释放两次,C++文档说明这是个未定义行为,所以不同编译器可能处理手段不一样,我的gcc 7.4.0 竟然没有报错。后来我又在网上的一些在线编译器实验一下,有的会报错,有的不会。

所以结论就是:类的构造函数需要申请堆内存的时候,我们要进行赋值运算符的重载,下面讲如何重载。

如何重载赋值运算符

#include <iostream>

using namespace std;

class person{

int* age;

public:

person(const int& a=10); //构造函数

~person(); //析构函数

void showAdd(); //打印age的地址

void show(); //打印age指向的值

void set(const int& a){*age=a;} //设置age指向的值

void operator=(person const& e); //重载赋值运算符

};

void person::operator=(person const& e)

{

if(age) delete age; //如果原先age申请过堆内存,要先释放

int data = *(e.age);

age = new int(data);

}

person::person(const int& a) {age = new int(a);}

person::~person(){delete age; cout<<"析构\n";}

void person::showAdd() {cout << hex << age<<endl;}

void person::show() {cout<<*age<<endl;}

void f(person& a) {

person b;

b = a; //这时候b指向了一块新的空间

a.show();

b.show();

a.showAdd();

b.showAdd();

//因为b是局部变量,所以进入main函数之前,b会自动调用析构函数

}

int main() {

person a(11);

f(a);

cout<<"进入main函数\n";

a.set(9); //因为b释放的指针和age指向不一样,set不会出错

return 0;

}

程序运行正常,内存图如下:

注意上边我用的operator=返回值是void, 这样不能进行连续赋值,比如: person a = b = c; ,若想连续赋值,返回值要声明为 引用

person& person::operator=(person const& e)

{

if(age) delete age;

int data = *(e.age);

age = new int(data);

return *this;

}

关于拷贝函数

再回看一下上边的代码,我的声明语句和赋值语句是分开的 person b; b=a; ,如果声明时赋值 person b=a; ,那么调用的函数就不是 operator= 了,而是拷贝函数

class person{

int* age;

public:

person(person const& e); //这就是拷贝函数

}

需要注意的是:上边说的operator返回值有两种情况:void和引用,其实还有第三种,既然能返回引用那就还能返回值:

person person::operator=(person const& e)

{

if(age) delete age;

int data = *(e.age);

age = new int(data);

return *this;

}

函数返回值的时候会临时构造一个 person 变量, 这个变量的 age 的指向和调用 operator= 的对象的 age 指向一样,也就是:

operator= 调用完之后,临时变量会调用析构函数,从而导致和上边一样的错误,doublefree。所以 operator= 的返回值最好是引用!

到此这篇关于C++ 类的赋值运算符'='重载的方法实现的文章就介绍到这了,更多相关C++ 类的赋值运算符'='重载内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

以上是 C++ 类的赋值运算符&#039;&#039;=&#039;&#039;重载的方法实现 的全部内容, 来源链接: utcz.com/p/244863.html

回到顶部