473,412 Members | 2,054 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,412 software developers and data experts.

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 1819

"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
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
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
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
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
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
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
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
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
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
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.