类中重载赋值运算符

重载赋值运算符时,先根据传入的地址判断是否与自己相等,如果相等,则返回*this。否则,释放旧空间,申请新空间,复制数据,返回*this。 
#include <iostream>
#include <cstring>
using namespace std;

class CMessage{
private:
	char* pMessage;
public:
	CMessage(const char* text = "Default message"){
		pMessage = new char[strlen(text) + 1];
		strcpy_s(pMessage, strlen(text) + 1, text);
	}
	CMessage& operator=(const CMessage& aMess){
		if (this == &aMess)
			return *this;
		delete []pMessage;
		pMessage = new char[strlen(aMess.pMessage) + 1];
		strcpy_s(this->pMessage, strlen(aMess.pMessage) + 1, aMess.pMessage);
		cout << aMess.pMessage << endl;
		return *this;
	}
	
};
int main(){
	CMessage motto1("An apple");
	CMessage motto2;
	motto2 = motto1;
	system("pause");
	return 0;
}
删除空指针是被允许的,因此可以delete pMessage;
1.赋值运算符函数返回的是引用
假如返回的是对象而不是引用,则对于(motto1 = motto2) = motto3;翻译为赋值操作为(motto1.operator=(motto2)).operator=(motto3);如果返回类型仅仅是CMessage,则该语句不合法。因为实际返回的是原始对象的临时副本,编译器不允许使用临时对象调用成员函数。因此如果希望实现使用赋值运算符处理类对象的灵活性,则返回类型必须是CMessage&。
2.赋值运算符函数必须检查是否与自己相同
如果不通过地址检查是否与自己相同,则对于motto1=motto1;这种情况下,赋值运算符函数将释放motto1的内存,然后分配新内存,然后复制新数据。但是释放旧内存的时,已经将新的数据删除,因此发生错误。

类的对象赋值运算符时,不一定调用重载赋值运算符的函数。例如对象初始化时,可能调用复制构造函数。

#include <iostream>
using namespace std;

class Test{
public:
	Test(int n = 10) : num(n){
		cout << "constructor called" << endl;
	}
	Test(const Test& aTest){
		this->num = aTest.num;
		cout << "copy constructor called" << endl;
	}
	Test& operator = (const Test& aTest){
		this->num = aTest.num;
		cout << "Assign constructor called" << endl;
		return *this;
	}
private:
	int num;
};

int main(){
	int count = 1;
	cout << count++ << "  ";
	Test t1(20);
	cout << count++ << "  ";
	Test t2;
	t2 = t1;
	cout << count++ << "  ";
	Test t3 = t1;
	cout << endl;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值