条款10:令operator=返回一个reference to *this
2012-11-15 22:30
477 查看
关于赋值,我们知道可以连续的进行赋值如:
int x, y, z; x = y = z = 12;
为了实现连锁的赋值,赋值操作符必须返回一个reference指向操作符的左侧实参,这是你要为class实现赋值操作符时,应该遵循的协议:
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;
}
}
请记住:
令赋值操作符返回一个reference to *this
int x, y, z; x = y = z = 12;
为了实现连锁的赋值,赋值操作符必须返回一个reference指向操作符的左侧实参,这是你要为class实现赋值操作符时,应该遵循的协议:
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;
}
}
请记住:
令赋值操作符返回一个reference to *this
相关文章推荐
- 《Effect C++》学习------条款10:令 operator= 返回一个 reference to *this
- 条款10:令operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- Effective C++ 条款10解读: 令operator= 返回一个reference to *this
- Effective C++ 条款10:令operator= 返回一个reference to *this
- Effective C++ 条款10: 令operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to * this
- 条款10-11:令operator=返回一个reference to *this
- effective C++ 条款 10:operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10 令operator=返回一个 reference to *this
- 条款10:令operator= 返回一个reference to *this
- Effective C++:条款10:令operator=返回一个reference to *this。
- 《Effective C++ 》条款10:令operator=返回一个reference to *this
- 条款10:令operator = 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator= 返回一个reference to *this
- 条款10 令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to * this(Have assignment operators return a reference to *this)