C++ operator = overloading用copy & swap有啥优点# Programming - 葵花宝典
m*a
1 楼
简单的返回一个right hand value?
所以不能支持(a=b)=c:
MyClass MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
返回一个reference,可以用 left hand value, 支持(a=b)=c:
MyClass & MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
所以不能支持(a=b)=c:
MyClass MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
返回一个reference,可以用 left hand value, 支持(a=b)=c:
MyClass & MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}