473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overload ambiguous when it shouldn't be?

Hi,

the following code produces an error on the second-last line:

Interface Left
Sub op()
End Interface

Interface Right
Sub op(ByVal i As Integer)
End Interface

Public Interface LR
Inherits Left, Right
End Interface

Class Derived
Implements LR

Public Overloads Sub op() Implements Left.op
End Sub

Public Overloads Sub op(ByVal i As Integer) Implements Right.op
End Sub
End Class

Sub test()
Dim d As LR = New Derived
d.op() 'op' is ambiguous across the inherited interfaces 'Client.Left' and 'Client.Right'.
End Sub

I don't see what's ambiguous here. One operation has a parameter, and one does not have
a parameter, so there is no ambiguity. Interestingly, replacing the declaration of d with:

Dim d As Derived = New Derived

makes the error disappear. But I don't see why a call on a Derived would be
any less ambiguous than a call on an LR -- both have the same methods with the
same signatures.

Compiler bad?

Cheers,

Michi.
Nov 21 '05 #1
2 1823

"Michi Henning" <mi***@zeroc.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Hi,

the following code produces an error on the second-last line:

Interface Left
Sub op()
End Interface

Interface Right
Sub op(ByVal i As Integer)
End Interface

Public Interface LR
Inherits Left, Right
End Interface

Class Derived
Implements LR

Public Overloads Sub op() Implements Left.op
End Sub

Public Overloads Sub op(ByVal i As Integer) Implements Right.op
End Sub
End Class

Sub test()
Dim d As LR = New Derived
d.op() 'op' is ambiguous across the inherited interfaces 'Client.Left'
and 'Client.Right'.
End Sub


That is correct behaviour. Read this document:
http://msdn.microsoft.com/library/de...bspec4_2_2.asp
According to this document:
"Unlike other types, which only derive from a single base type, an interface
may derive from multiple base interfaces. Because of this, an interface can
inherit an identically named type member from different base interfaces. In
such a case, the multiply inherited name is not available in the derived
interface, and referring to any of those type members through the derived
interface causes a compile-time error, regardless of signatures or
overloading. Instead, conflicting type members must be referenced through a
base interface name."

So, you should be doing something like:

Class Derived
Implements LR

' note that you dont even need the Overloads keyword
Public Sub op() Implements Left.op

End Sub

Public Sub op(ByVal i As Integer) Implements Right.op

End Sub
End Class

Sub Test()
Dim d As LR = New Derived

' access the op from Left
DirectCast(d, Left).op()

' access the op from Right
Dim i As Integer
DirectCast(d, Right).op(i)

End Sub
hope that helps..
Imran.
Nov 21 '05 #2
"Imran Koradia" <no****@microsoft.com> wrote in message
news:uG***************@TK2MSFTNGP09.phx.gbl...

That is correct behaviour. Read this document:
http://msdn.microsoft.com/library/de...bspec4_2_2.asp According to this document:
"Unlike other types, which only derive from a single base type, an interface
may derive from multiple base interfaces. Because of this, an interface can
inherit an identically named type member from different base interfaces. In
such a case, the multiply inherited name is not available in the derived
interface, and referring to any of those type members through the derived
interface causes a compile-time error, regardless of signatures or
overloading. Instead, conflicting type members must be referenced through a
base interface name."
I agree -- the compiler behaves as the doc says it should.

I don't understand the motivation though. Why would overload
resolution not apply to interfaces, but apply to classes?
So, you should be doing something like:
[...]

Sub Test()
Dim d As LR = New Derived

' access the op from Left
DirectCast(d, Left).op()

' access the op from Right
Dim i As Integer
DirectCast(d, Right).op(i)

End Sub


Sure, the cast fixes it. But requiring the case appears
to be pointless. In particular, if I write:

Dim D As Derived = New Derived
d.op() ' No error here

then the compiler is quite content to apply overload resolution
and does exactly what you would expect.

So, why should it matter whether the reference is of class
type or interface type? Either way, the same set of method
candidates is available, so why apply overload resolution
in one case but not the other?

Cheers,

Michi.

--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4999
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
25
2784
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
0
278
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
9
2363
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
7
14888
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
6
1794
by: Jonas Huckestein | last post by:
hello, somehow i can't figure out, how to overload the operator for a referenced object. if i have class MyClass { int operator(int i) { return 1; }; };
11
4623
by: onkar | last post by:
#include<iostream> using namespace std; class Integer{ int i; public: Integer(int ii):i(ii){} const Integer operator+(const Integer& rv){ cout<<"1-operator+"<<endl; return Integer(i+rv.i); }
3
5791
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...
2
1847
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
9
2185
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.