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

how to resolve ambiguous overloaded operators

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


P: n/a

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.