473,414 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Help overloading + and += simultaneously??

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.

--
John Hardin KA7OHZ ICQ#15735746 http://www.impsec.org/~jhardin/
jh*****@impsec.org FALaholic #11174 pgpk -a jh*****@impsec.org
key: 0xB8732E79 - 2D8C 34F4 6411 F507 136C AF76 D822 E6E6 B873 2E79
Nov 16 '05 #1
8 1335
"John Hardin" <jh*****@impsec.org> wrote in
news:pa**************************@impsec.org...
... 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
Nov 16 '05 #2
"John Hardin" <jh*****@impsec.org> wrote:
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.
Nov 16 '05 #3
On 27 Jun 2004 17:43, "John Hardin" wrote:
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...)

--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #4
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.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Simon Smith" <gh*****@community.nospam> wrote in message
news:44******************************@ghytred.com. ..
On 27 Jun 2004 17:43, "John Hardin" wrote:
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 andyou 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. Cansomebody 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...)

--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook

Nov 16 '05 #5
Simon Smith sez:
On 27 Jun 2004 17:43, "John Hardin" wrote:
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.


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

--
John Hardin KA7OHZ ICQ#15735746 http://www.impsec.org/~jhardin/
jh*****@impsec.org FALaholic #11174 pgpk -a jh*****@impsec.org
key: 0xB8732E79 - 2D8C 34F4 6411 F507 136C AF76 D822 E6E6 B873 2E79

Nov 16 '05 #6
> 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).

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #7
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #8
"cody" <no****************@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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).


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!
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
12
by: Joe | last post by:
Hi, Can I pass a "generic" class pointer as an argument to a function? For instance say classA and B are both derived from Z. { int iType =1;
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
4
by: Madhu Gopinathan | last post by:
Hi All, I am faced with a horrible hang problem. I have a COM exe server that executes some tasks. The task execution manager is a thread that manages the pool of threads, which is 4 per processor....
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
3
by: AVL | last post by:
Hi, I need some info on text and xml files...... How many users can simultaneously read a text or xml file... I've a web appliaction which needs to access a text file and a xml file.. The...
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
35
by: josh | last post by:
Hi, I coded the following but It does not return what I expect, why? #include <iostream> using namespace std; class Other { public: int i;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.