Strange thing about operator overloads beings static

H

Harlan Messinger

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?
 
1

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
 
J

Jon Skeet

Harlan Messinger said:
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?)
 
H

Harlan Messinger

100 said:
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.
 

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