这样做是为了能连等于,下面是个例子。
class Number{
public:
Number& operator=(int num){
number = num;
return *this;
}
private:
int number;
};
int main() {
Number a, b, c;
a = b = c = 1;
a = (b = (c = 1));
}
像+=,*= ,/=这种带有=性质的操作符,最好都返回一个*this