473,382 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 9963

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
7
by: ishekara | last post by:
Hi, I am having a class template which is used to convert from one type another. I am having a problem when i use the copy constructor with same type. code. #include "stdio.h" template...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
5
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.