effective C++ 条款 10:operator= 返回一个reference to *this
2012-01-13 16:46
549 查看
赋值的连锁式 x=y=z=3;被解析成x=(y=(z=3));为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参,
这是你为class实现赋值操作符时应该遵循的协议:
class Widget
{
public:
Widget& operator=(const Widget& rhs) //返回类型是reference,
{ //指向当前对象
...
return *this; //返回左侧对象
}
protected:
private:
};
这个协议不仅适用于以上的标准赋值形式,也适用于所有赋值相关运算。
class Widget
{
public:
Widget& operator+=(const Widget& rhs) //返回类型是reference,
{ //指向当前对象
...
return *this; //返回左侧对象
}
Widget& operator=(int rhs)
{
...
return *this;
}
protected:
private:
};
令赋值操作符返回一个reference to *this。
class Widget
{
public:
Widget& operator=(const Widget& rhs) //返回类型是reference,
{ //指向当前对象
...
return *this; //返回左侧对象
}
protected:
private:
};
这个协议不仅适用于以上的标准赋值形式,也适用于所有赋值相关运算。
class Widget
{
public:
Widget& operator+=(const Widget& rhs) //返回类型是reference,
{ //指向当前对象
...
return *this; //返回左侧对象
}
Widget& operator=(int rhs)
{
...
return *this;
}
protected:
private:
};
相关文章推荐
- 《Effective C++》学习笔记条款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
- 《Effective C++ 》条款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
- Effective C++:条款10:令operator=返回一个reference to *this。
- 读书笔记《Effective c++》 条款10 令operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this(Have assignment operators return a reference to *this.)
- 条款10:令operator=返回一个reference to *this
- 条款10-11:令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)
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- effective C++ 10_令operator=返回一个reference to *this 读书笔记
- 条款10 令operator=返回一个reference to *this