472,126 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

how to resolve ambiguous overloaded operators

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
Nov 15 '05 #1
1 9725

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: az********@nanoink.net (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: <6d**************************@posting.google.com >
| 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: gr**********@google.com
| NNTP-Posting-Date: Tue, 7 Oct 2003 17:08:47 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.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
|

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andy Jarrell | last post: by
20 posts views Thread by Brad Eck | last post: by
4 posts views Thread by masood.iqbal | last post: by
7 posts views Thread by ishekara | last post: by
9 posts views Thread by neildferguson | last post: by
3 posts views Thread by i3x171um | last post: by
reply views Thread by leo001 | last post: by

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.