The first part is easy to understand: scope issue
The second part is important because it will lead to memory leak. (Related to item 23 and operator overload)
The first part is easy to understand: scope issue
The second part is important because it will lead to memory leak. (Related to item 23 and operator overload)
Revisiting Item 23: Don’t Try to Return a Reference When You Must Return an Object
While the title sounds rather generic, the main content is about what is appropriate to return from various operator functions.
SO has a more
In the case of binary operators, here is a good example of the function declaration/implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Note the parameter lhs is passed into operator+
via copying.
For example, one should try to avoid
Returning a handle of a private member variable via public member functions
Returning a pointer of a private function via public interface
Item 29 is to adress situations in which the holder of a handle (a pointer or reference to the internal data representation) can circumvent the const
semantic by making modification to the underlying data via the handle.
To disallow an operation on your object class, you can
1) declare the function as private
2) do not provide the implementation (if called, caused link error. Prevent being friended)