By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,241 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,241 IT Pros & Developers. It's quick & easy.

Strange thing about operator overloads beings static

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
Hi Harlan,

Create an example of your own class A.

Regards,
Fergus
Nov 15 '05 #4

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.