通过参考和操作员链接返回
Return by reference and operator chaining
为什么要返回操作员链接?
我在这样看了很多答案,库存短语是"通过参考返回允许操作员链接",但是没有解释,我不明白为什么我不能以其他方式做。p>例如,这似乎在我的系统上编译并运行良好,链接起作用:
TestClass operator+=(TestClass tc){
somePrivateValue += tc.somePrivateValue;
return *this;
}
我也可以做到这一点:
TestClass& operator+=(TestClass& tc){
somePrivateValue += tc.somePrivateValue;
return *this;
}
排除第二个示例不需要为TestClass
调用复制构造函数的其他优点是什么?
和链接作品
不,不是。
TestClass a = some_value;
TestClass b = some_other_value;
(a += b) += b;
现在检查a
的值,但是完成了。它反映了两次被b
递增吗?或仅一次?
相关文章:
- 返回的指向C++对象的链接是什么样的
- 返回一个带有 std::move 的对象并链接函数
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 从两个 lambda 的函数返回 lambda 时的链接器错误
- 在索引中插入节点:链接列表未正确返回列表
- 从函数中的指针返回对象,例如链接列表
- 为什么 VS C++链接器返回具有混合 DLL 转发的"LNK2001: unresolved external symbol"?
- 如何返回指向链接列表中最大值的指针
- C++链接列表头返回 NULL,即使列表不为空
- 在对类对象的赋值进行链接时获取垃圾值,使用按值返回类对象的赋值运算符重载
- 返回值上的 C++ 方法链接不起作用
- C++ 链接器错误未定义对"Suma(int, int)"的引用 ld 返回 1 个退出状态
- 链接错误:收集 2:错误:ld 返回 1 个退出状态
- 尽管返回了引用,但无法链接重载的赋值运算符
- 链接错误(collect2:ld返回1)
- 链接列表中的虚函数 - 多种返回类型(对象)
- 通过参考和操作员链接返回
- 链接不返回对象的运算符
- 方法调用链接;返回指针与引用
- Boost::filesystem::is_empty()对于符号链接返回false