J
John Hardin
All:
The syntax for overloading the "+" and other simple binary math operators
is pretty straightforward, but I can't seem to wrap my brain around the
idea that overloading the "+" operator simultaneously overloads the "+="
operator.
Here's the problem I'm having with it:
When you overload a binary operator like "+", you accept two arguments and
you return a new object that holds the result. That's straightforward.
But if the overload of the "+" operator returns a *new* object, how can
that same overload be used to implement the "+=" operator, which is a
change to an *existing* object?
I just can't see how you can overload "+" and "+=" with the same code. Can
somebody help me understand this by pointing me to where this is
explained, or by explaining it to me?
Thanks.
The syntax for overloading the "+" and other simple binary math operators
is pretty straightforward, but I can't seem to wrap my brain around the
idea that overloading the "+" operator simultaneously overloads the "+="
operator.
Here's the problem I'm having with it:
When you overload a binary operator like "+", you accept two arguments and
you return a new object that holds the result. That's straightforward.
But if the overload of the "+" operator returns a *new* object, how can
that same overload be used to implement the "+=" operator, which is a
change to an *existing* object?
I just can't see how you can overload "+" and "+=" with the same code. Can
somebody help me understand this by pointing me to where this is
explained, or by explaining it to me?
Thanks.