473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ 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 1850

"Michi Henning" <mi***@zeroc.co m> wrote in message
news:uC******** ******@TK2MSFTN GP09.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****@microso ft.com> wrote in message
news:uG******** *******@TK2MSFT NGP09.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
5012
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, std::char_traits<char> >& << Thing&' operator friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&...
25
2850
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 contents" ) ; row << new MyTableCell( "cell2 text contents" ) ; table << row ; thanks,
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
2382
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 attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
7
14919
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 having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
6
1813
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
4644
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
5820
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 long long int), a decimal (double), or a string (std::string). I basically want to overload most of a Message's operators so that the Message class feels more like a native type. For example... Message msg = "tester"; std::string str = msg;...
2
1867
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
2196
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
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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 we have to send another system

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.