473,396 Members | 2,061 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,396 software developers and data experts.

Strange thing about operator overloads beings static

Since operator overloads into static functions in C#, there really doesn't
need to be a connection between the types to which the operator is being
applied and the type in which the overload is defined, does there? I mean,
there's nothing to prevent the following, right?

public class A
{
public static B operator + (C c, D d) { ... }
...
}

Seems strange, somehow.

Suppose I'm using utilities provided by several different graphics library
developers in a graphics application of mine. Suppose that two of those
developers thought that it would be clever to have

public static Bitmap operator + (Bitmap x, Bitmap y)

which returns a Bitmap that consists of the two original graphics laid out
side by side. One of them defines this in a class called MyUtilities, the
other in a class called Blech. What would happen? Would there be a
work-around?

--
Harlan Messinger
Remove the first dot from my e-mail address.
Veuillez ôter le premier point de mon adresse de courriel.

Nov 15 '05 #1
4 1286
100
Hi Harlan,
The following is a quote form the c# standard:
"User-defined operator declarations always require at least one of the
parameters to be of the class or struct type that contains the operator
declaration."

I think this answers your question.

HTH
B\rgds
100

"Harlan Messinger" <h.*********@comcast.net> wrote in message
news:bi************@ID-114100.news.uni-berlin.de...
Since operator overloads into static functions in C#, there really doesn't
need to be a connection between the types to which the operator is being
applied and the type in which the overload is defined, does there? I mean,
there's nothing to prevent the following, right?

public class A
{
public static B operator + (C c, D d) { ... }
...
}

Seems strange, somehow.

Suppose I'm using utilities provided by several different graphics library
developers in a graphics application of mine. Suppose that two of those
developers thought that it would be clever to have

public static Bitmap operator + (Bitmap x, Bitmap y)

which returns a Bitmap that consists of the two original graphics laid out
side by side. One of them defines this in a class called MyUtilities, the
other in a class called Blech. What would happen? Would there be a
work-around?

--
Harlan Messinger
Remove the first dot from my e-mail address.
Veuillez ôter le premier point de mon adresse de courriel.

Nov 15 '05 #2
Harlan Messinger <h.*********@comcast.net> wrote:
Since operator overloads into static functions in C#, there really doesn't
need to be a connection between the types to which the operator is being
applied and the type in which the overload is defined, does there? I mean,
there's nothing to prevent the following, right?

public class A
{
public static B operator + (C c, D d) { ... }
...
}
Well, there's the C# spec which specifies:

<quote>
User-defined operator declarations always require at least one of the
parameters to be of the class or struct type that contains the operator
declaration.
</quote>

It makes sense to have some overloaded operator parameters not being
the same type though - for instance, DateTime+TimeSpan makes perfect
sense.
Suppose I'm using utilities provided by several different graphics library
developers in a graphics application of mine. Suppose that two of those
developers thought that it would be clever to have

public static Bitmap operator + (Bitmap x, Bitmap y)

which returns a Bitmap that consists of the two original graphics laid out
side by side. One of them defines this in a class called MyUtilities, the
other in a class called Blech. What would happen? Would there be a
work-around?


Supposing you *could* do that: how would you expect it to be called?
(ie what syntax would you expect would generate a call to it?)

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

Create an example of your own class A.

Regards,
Fergus
Nov 15 '05 #4

"100" <10*@100.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
Hi Harlan,
The following is a quote form the c# standard:
"User-defined operator declarations always require at least one of the
parameters to be of the class or struct type that contains the operator
declaration."

I think this answers your question.


That helps, thanks! The book just didn't mention it, unless I missed it.

Nov 15 '05 #5

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

Similar topics

3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
3
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am...
2
by: Leszek Taratuta | last post by:
Hello, I posted this problem a few days ago. I am not sure if I was not enough specific but nobody answered, so I am posting it again: I have the following code snippet that overloads the "-"...
3
by: Jesper | last post by:
Hi, I've made a class with the following operator overloads listed below. However, If I test a 'pointer' for an instace, I get an exception that the object is not set to an insstance of an...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be...
6
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the...
17
by: My interest | last post by:
How can I use * operator in C# generics? e.g. class foo<T_> { T_ v1, v2: public T_ squar() { return v1 * v2; /// wrong! }
8
by: Paul E Collins | last post by:
This code won't compile because "the modifier 'abstract' is not valid for this item". abstract class X { public abstract static bool operator >(X a, X b); public abstract static bool operator...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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,...

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.