My C++ lecturer claims that whenever you have to implement the operator+() you must provide operator+=() as well?
I could not understand this logic, can someone explain me the logic behind this decision?
My C++ lecturer claims that whenever you have to implement the operator+() you must provide operator+=() as well?
I could not understand this logic, can someone explain me the logic behind this decision?
Your lecturer is wrong.
The fact that you can sum two objects forming a new third object (which is what binary +
does) does not necessarily imply that your objects are supposed to be modifiable "in place" (which is what +=
does).
In other words, if and only if your class supports the concept of being modifiable in place, then it is a fairly good programming practice to provide +=
whenever you provide both +
and =
. But in any case there's no "must" here.