how to resolve ambiguous overloaded operators

A

Alex Zhitlenok

Hi,
My question is how to resolve in C# ambiguous overloaded operators?

Let say, I have two unrelated classes A and B, each one implements
overloaded operator + with the first parameter of type A, and the
second one of type B. Let say, these are not my classes and I know
nothing about the implementation. As system doesn't know what code
must be used for resolving the language construction a+b (where A a;
and B b;), it returns "The call is ambiguous between the following
methods or properties: 'B.operator +(A, B)' and 'A.operator +(A, B)'"
error message, that is perfectly expected.

The question is how to resolve the ambiguity? By the way, I even can
not implement my own method because, in general, I don't know the
algorithm of the special addition and the proper way is to call
operator+ against class A.

Here is the pseudo code:
public class A{
public static A operator+(A a, B b){
return a;
}
}
public class B{
public static A operator+(A a, B b){
return a;
}
}
A a = new A();
B b = new B();
Object o = a+b; // error "The call is ambiguous…"

Please, be sure this is just a theoretical question, so that I am not
looking for suggestions of how to improve my design.
Thank you,
Alex
 
J

Jeffrey Tan[MSFT]

Hi Alex,

The use of the operator overloading is easy to generate "ambiguous
overloaded operators" problem.
So you should design your logic well to not generate the ambiguous.
You can just delete one type of operator overloading.

I think there may be a problem, when the A and B are not designed by you,
so you can not change it implementaion.
But I think in this situation, the problem will not be you, but be the
designer of these 2 classes.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: (e-mail address removed) (Alex Zhitlenok)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: how to resolve ambiguous overloaded operators
| Date: 7 Oct 2003 10:08:46 -0700
| Organization: http://groups.google.com
| Lines: 36
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 68.72.66.153
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1065546527 21537 127.0.0.1 (7 Oct 2003
17:08:47 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Tue, 7 Oct 2003 17:08:47 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!npeer.de.kpn-eurorings.net!news-out.nuthinbutnews.com!propagator2-sterl
ing!In.nntp.be!tdsnet-transit!newspeer.tds.net!sn-xit-02!sn-xit-04!sn-xit-01
!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:189615
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
| My question is how to resolve in C# ambiguous overloaded operators?
|
| Let say, I have two unrelated classes A and B, each one implements
| overloaded operator + with the first parameter of type A, and the
| second one of type B. Let say, these are not my classes and I know
| nothing about the implementation. As system doesn't know what code
| must be used for resolving the language construction a+b (where A a;
| and B b;), it returns "The call is ambiguous between the following
| methods or properties: 'B.operator +(A, B)' and 'A.operator +(A, B)'"
| error message, that is perfectly expected.
|
| The question is how to resolve the ambiguity? By the way, I even can
| not implement my own method because, in general, I don't know the
| algorithm of the special addition and the proper way is to call
| operator+ against class A.
|
| Here is the pseudo code:
| public class A{
| public static A operator+(A a, B b){
| return a;
| }
| }
| public class B{
| public static A operator+(A a, B b){
| return a;
| }
| }
| A a = new A();
| B b = new B();
| Object o = a+b; // error "The call is ambiguous…"
|
| Please, be sure this is just a theoretical question, so that I am not
| looking for suggestions of how to improve my design.
| Thank you,
| Alex
|
 

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