return value of operator overloading in C++
2015-07-27 17:39
519 查看
up vote25down votefavorite 8 | I have a question about the return value of operator overloading in C++. Generally, I found two cases, one is return-by-value, and one is return-by-reference. So what's the underneath rule of that? Especially at the case when you can use the operator continuously, such as cout<<x<<y. For example, when implementing a + operation "string + (string)". how would you return the return value, by ref or by val. c++ operator-overloading
| |||
add a comment |
5 Answers
activeoldestvotesup vote33down voteaccepted | Some operators return by value, some by reference. In general, an operator whose result is a new value (such as +, -, etc) must return the new value by value, and an operator whose result is an existing value, but modified (such as <<, >>, +=, -=, etc), should return a reference to the modified value. For example, coutis a std::ostream, and inserting data into the stream is a modifying operation, so to implement the <<operator to insert into an ostream, the operator is defined like this: std::ostream& operator<< (std::ostream& lhs, const MyType& rhs) { // Do whatever to put the contents of the rhs object into the lhs stream return lhs; } This way, when you have a compound statement like cout << x << y, the sub-expression cout << xis evaluated first, and then the expression [result of cout << x ] << yis evaluated. Since the operator <<on xreturns a reference to cout, the expression [result of cout << x ] << yis equivalent to cout << y, as expected. Conversely, for "string + string", the result is a new string (both original strings are unchanged), so it must return by value (otherwise you would be returning a reference to a temporary, which is undefined behavior).
| ||||||||||||||||||||
comments |
up vote12down vote | To attempt an answer to your question regarding strings, the operator+() for strings is almost always implemented as a free (non-member) function so that implicit conversions can be performed on either parameter. That is so you can say things like:string s1 = "bar"; string s2 = "foo" + s1; Given that, and that we can see that neither parameter can be changed, it must be declared as: RETURN_TYPE operator +( const string & a, const string & b ); We ignore the RETURN_TYPE for the moment. As we cannot return either parameter (because we can't change them), the implementation must create a new, concatenated value: RETURN_TYPE operator +( const string & a, const string & b ) { string newval = a; newval += b; // a common implementation return newval; } Now if we make RETURN_TYPE a reference, we will be returning a reference to a local object, which is a well-known no-no as the local object don't exist outside the function. So our only choice is to return a value, i.e. a copy: string operator +( const string & a, const string & b ) { string newval = a; newval += b; // a common implementation return newval; }
| ||||
a comment |
up vote3down vote | If you want your operator overload to behave like the built-in operator, then the rule is pretty simple; the standard defines exactly how the built-in operators behave and will indicate if the result of a built-in is an rvalueor an lvalue. The rule you should use is: if the built-in operator returns an rvaluethen your overload should return a reference if the built-in returns an lvaluethen your overload should return a value However, your overload isn't required to return the same kind of result as the built-in, though that's what you should do unless you have a good reason to do otherwise. For example, KennyTM noted in a comment to another answer that the stream overloads for the <<and >>operators return a reference to the left operand, which is not how the built-ins work. But the designers of the stream interface did this so stream I/O could be chained.
| ||||
a comment |
up vote2down vote | Depending on the operator you may have to return by value. When both can be used though, like in operator+= you could consider the following: If your objects are immutable it's probably better to return by value. If your objects are mutable it's probably better to return by reference.
| ||
add a comment |
up vote0down vote | Usually you return by reference in an operation that changes the value of the things it's operating on, like =or +=. All other operations are return by value. This is more a rule of thumb, though. You can design your operator either way.
|
相关文章推荐
- C/C++__如何创建一个空字符串数组
- 3、C/C++中的const专题
- VS2008 C++ 错误 fatal error C1853: "Debug\sift.pch"
- c语言 字符版 简易2048
- C++Primer小结(1)
- C++学习笔记(类型转换)
- C++11中的匿名函数(lambda函数,lambda表达式) (转)
- c++ 使用全局变量的方法多个文件
- C++第二节(1):public和private、初始化列表、函数重载
- 面向 C++ 的测试驱动开发
- LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)
- VC: 更改MFC程序图标
- 安装cpplint
- C++技巧之STL(持续更新)
- 数字滤波器实现(mantlab+C语言)
- C语言链表(1)——搜索
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- c++模板
- C++构造函数与传默认值的关系
- IOS开发系列--C语言之存储方式和作用域