The solution to GotW #4 is now available:
GotW #4 Solution: Class Mechanics (updated for C++11/14)
by Herb Sutter
From the article:
... To see why, consider the following canonical forms for howoperator+=
andoperator+
should normally be implemented for some typeT
.T& T::operator+=( const T& other ) { //... return *this; }
T operator+( T a, const T& b ) { a += b; return a; }Did you notice that one parameter is passed by value, and one by reference? That’s because if you’re going to copy from a parameter anyway, it’s often better to pass it by value, which will naturally enable a move operation if the caller passes a temporary object such as in expressions like
(val1 * val2) + val3
. We’ll see more on parameter passing in a future GotW. ...
.
Add a Comment
Comments are closed.