問題聚焦:
這個准則比較簡短,但是往往就是這種細節的地方,可以提高你的代碼質量。 細節決定成敗,讓我們一起學習這條重載賦值操作符時需要遵守的准則吧。
// 連鎖賦值 x = y = z = 15; // 上面的表達式等價於 x = ( y = ( z = 15 ) );
class Widget {
public:
Widget& operator=(const Widget& rhs)
{
....
return* this;
}
};
class Widget {
public:
...
Widget& operator+=(const Widget& rhs)
{
....
return *this;
}
Widget& operator=(const Widget& rhs)
{
...
return *this;
}
};