您的位置:首页 > Web前端

条款10:令operator=返回一个refereglce to this

2017-08-07 23:01 281 查看
// 条款10: 令operator=返回一个reference to *this。
// Have assignment operators return a reference to *this.

// 这只是个良好的建议,这个建议被所有的内置类型和标准程序库提供的类型共同遵守。
// 这个建议同样适用于+=,-=,*=等等。
// 例如:
class Widgets {
public:
Widgets& operator+=(const Widgets& widgets) {
// 实现
return *this;
}
Widgets& operator=(int rhs) {  // 此函数同样适用,即时此操作符了参数类型不符协定。
// 实现
return *this;
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ Effective C++