hit counter

Timeline

My development logbook

Item 23 Revisited

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-depth treatment

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
class X {
  X& operator+=(const X& rhs)
  {
    // actual addition of rhs to *this 
    return *this;
  }
};
inline X operator+(X lhs, const X& rhs)
{
  lhs += rhs;
  return lhs;
}
};

Note the parameter lhs is passed into operator+ via copying.