Help overloading + and += simultaneously??

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.
 
N

Niki Estner

John Hardin said:
... 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?

Probably the C# designers had primarily value types and immutable reference
types in mind. Take for examples:

string a,b;
a = b = "Test-String";
a += "Suffix";
Console.WriteLine("a = {0}, b = {1}", a,b);

or

int a,b;
a = b = 5;
a += 1;
Console.WriteLine("a = {0}, b = {1}", a,b);

In both cases "a" will be modified, but "b" will not.
C# will mimic exactly that behaviour if you override the "+" operator.

If you're used to C-style "p = &x; *p += 2;" statements, this might be
counter-intuitive, but if you're honest, that kind of code was always
error-prone, and got quite rare due to C#'s stricter type system anyway.

Niki
 
P

Paul E Collins

John Hardin said:
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?

Because a += b is just shorthand for a = a + b.

P.
 
S

Simon Smith

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?

Because '+=' is syntactic sugar: in other words is something that the
compiler takes in and translates to something else (just like properties)
before before spitting out the appropriate MSIL. If you write 'i += j'
what the compiler actually compiles is 'i = i + j' and thus the + operator
is on it's own. IIRC '++' does the same (I think...)
 
J

John Wood

x++ and x+=1 and x=x+1 do amount to the same thing when compiled.

IL_000c: ldloc.0
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0

Each of those operations compile to exactly the same IL as seen above.
 
J

John Hardin

Simon Smith sez:
Because '+=' is syntactic sugar: in other words is something that the
compiler takes in and translates to something else (just like
properties) before before spitting out the appropriate MSIL. If you
write 'i += j' what the compiler actually compiles is 'i = i + j' and
thus the + operator is on it's own.

Thanks everybody who responded. It makes sense now.

Ouch. That means you have to be really careful overloading operators for
complex classes, and have to think carefully about precisely *what* you
mean by "+" and "+=" when designing classes.

For example, if you have a class with a single numeric property and
several non-numeric properties, you would need to copy all the non-numeric
properties to your new class in the overloaded "+" code lest they get
discarded - you can't distinguish between "a = a + b" and "a = b + c" at
runtime, can you?

Maybe operator overloading is just a Bad Idea for nontrivial classes...
 
C

cody

x++ and x+=1 and x=x+1 do amount to the same thing when compiled.
IL_000c: ldloc.0
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0

Each of those operations compile to exactly the same IL as seen above.

That has nothing to do with it. The operator+ for primitive types is a
single opcode in IL (add), and for user-defined types it is a call to a
method called op_Addition(x, y).
 
C

cody

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?


The operator overloading in .NET seems very unready. Indeed, what you want
is currently not possible.
It is the same reason why the class StringBuilder does't support operator+=,
but the class string does.
 
J

John Wood

cody said:
That has nothing to do with it. The operator+ for primitive types is a
single opcode in IL (add), and for user-defined types it is a call to a
method called op_Addition(x, y).

Follow the thread man. The point here is that there is no difference between
the 3 addition operations above, which is what Simon was saying. Plus it's
interesting information, if you don't find it interesting don't comment!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top