重载赋值运算符时,先根据传入的地址判断是否与自己相等,如果相等,则返回*this。否则,释放旧空间,申请新空间,复制数据,返回*this。
1.赋值运算符函数返回的是引用
假如返回的是对象而不是引用,则对于(motto1 = motto2) = motto3;翻译为赋值操作为(motto1.operator=(motto2)).operator=(motto3);如果返回类型仅仅是CMessage,则该语句不合法。因为实际返回的是原始对象的临时副本,编译器不允许使用临时对象调用成员函数。因此如果希望实现使用赋值运算符处理类对象的灵活性,则返回类型必须是CMessage&。
2.赋值运算符函数必须检查是否与自己相同
如果不通过地址检查是否与自己相同,则对于motto1=motto1;这种情况下,赋值运算符函数将释放motto1的内存,然后分配新内存,然后复制新数据。但是释放旧内存的时,已经将新的数据删除,因此发生错误。
#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;
}