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
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
![Wink ;) ;)](/styles/default/custom/smilies/wink.gif)
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