473,503 Members | 8,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator overloading

Sam
I want to overload the + operator for ArrayList for adding elements

public static int operator +(ArrayList l, Object o)

return (l.Add(o))
First I have discovered that the return value cannot be void and if I change it to something like int, I have to always write like this
int i = list + 3; // I don't need
The compiler doesn’t accept jus
list + 3
is there a workaround? I just want to write
list + 3
as a statement and not an expression

Nov 16 '05 #1
3 1661
Sam
I have done it this way
public static List operator +(List l, Object o)

l.Add(o)
return (l)
I have just checked the methods. First the normal way of adding to a list

l.Add(2)
it has this MSIL code

IL_0013: ldloc.
IL_0014: ldc.i4.
IL_0015: box [mscorlib]System.Int3
IL_001a: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object
IL_001f: po

Second with my operator overloa
l = l + 3
which gives this MSIL code

IL_0020: ldloc.
IL_0021: ldc.i4.
IL_0022: box [mscorlib]System.Int3
IL_0027: call class [SCollections]Sam.Collections.List SCollections]Sam.Collections.List::op_Addition(class [SCollections]Sam.Collections.List, object
IL_002c: stloc.

What do you think about the performance? Are they that much different

Nov 16 '05 #2
Sam <an*******@discussions.microsoft.com> wrote:

<snip>
What do you think about the performance? Are they that much different?


I wouldn't worry about the performance - I suspect it won't be very
significantly different. I'm more worried about the readability - this
is an unfamiliar use of an ArrayList to most .NET programmers. Is this
code for just yourself, or for lots of people?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

"Sam" <an*******@discussions.microsoft.com> a écrit dans le message de
news:38**********************************@microsof t.com...
I want to overload the + operator for ArrayList for adding elements:

public static int operator +(ArrayList l, Object o)
{
return (l.Add(o));
}

First I have discovered that the return value cannot be void and if I change it to something like int, I have to always write like this: int i = list + 3; // I don't need i
The compiler doesn't accept just
list + 3;
is there a workaround? I just want to write
list + 3 ;
as a statement and not an expression.


My 2 cents: normally, an operator like +:

a) produces a result
b) does not modify its operands.

And you are trying to define a + operator that violates these 2 rules.

This is a bad idea. You should keep the method and only define operators
like + when you have a real algebra (operations that don't have side effects
on their operands). Otherwise, you will confuse the people who read your
code.

Bruno.
Nov 16 '05 #4

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

Similar topics

16
2565
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
6407
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
3058
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
2047
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
8575
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
2273
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
3607
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
3253
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
3474
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
2950
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7470
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
4693
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...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.