473,666 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange overloading behaviour

Here are three simple classes:

class A
{
public void DoIt(B b)
{
DoSomething(b);
}
public void DoSomething(B b)
{
}
public void DoSomething(C c)
{
}
}

class B
{
}

class C : B
{
}

Here is some simple code with line numbers:

1. A a = new A();
2. C c = new C();
3. a.DoSomething(c );
4. a.DoIt(c);

Here is my question:

Line 3 will cause the overloaded method A.DoSomething(C c) to be called, but
line 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wack
if you ask me. Yes, I can see that the first method to be called is DoIt(B
b), which then apparently causes the "wrong" subsequent method to be called.
But what is the underlying rationale for such behavior?

Nov 15 '05 #1
9 1652
Karahan Celikel <NO************ *******@hotmail .com> wrote:

<snip>
Here is my question:

Line 3 will cause the overloaded method A.DoSomething(C c) to be called, but
line 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wack
if you ask me. Yes, I can see that the first method to be called is DoIt(B
b), which then apparently causes the "wrong" subsequent method to be called.
But what is the underlying rationale for such behavior?


Because otherwise things would be hard to predict at compile time. The
method *signature* is chosen at compile time, and the actual method
invoked depends on overriding (not overloading).

This is certainly the way Java works as well, and I suspect it's how
C++ works (although I haven't checked).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Karahan Celikel wrote:
Line 3 will cause the overloaded method A.DoSomething(C c) to be
called, but line 4 causes A.DoSomething(B b) to be called. Why? That
seems pretty wack if you ask me. Yes, I can see that the first
method to be called is DoIt(B b), which then apparently causes the
"wrong" subsequent method to be called. But what is the underlying
rationale for such behavior?


Overloaded methods get called based upon the type and/or number of
arguments passed to them. DoIt expects an object of type B. A C object
is a B object so you can pass a C to DoIt. However, DoIt will cast that
object to an instance of the base class B. When DoIt calls DoSomething,
you get DoSomething(B b) not DoSomething(C c).

Where you call DoSomething directly, the same rules are followed.
However, since you're not casting your C object to an instance of its
base, the correct method is called and DoSomething(C c) gets executed.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #3
It is a drawback of C#.

Because overloaded method calls are determined at compile time rather than
runtime.

You ought to be able to designate overloaded methods as being "virtual" much
like overriden methods--so that the expected method is called.
"Karahan Celikel" <NO************ *******@hotmail .com> wrote in message
news:eu******** ******@TK2MSFTN GP09.phx.gbl...
Here are three simple classes:

class A
{
public void DoIt(B b)
{
DoSomething(b);
}
public void DoSomething(B b)
{
}
public void DoSomething(C c)
{
}
}

class B
{
}

class C : B
{
}

Here is some simple code with line numbers:

1. A a = new A();
2. C c = new C();
3. a.DoSomething(c );
4. a.DoIt(c);

Here is my question:

Line 3 will cause the overloaded method A.DoSomething(C c) to be called, but line 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wack
if you ask me. Yes, I can see that the first method to be called is DoIt(B b), which then apparently causes the "wrong" subsequent method to be called. But what is the underlying rationale for such behavior?

Nov 15 '05 #4
I agree with you that it is somewhat confusing. Case in
point: What behavior would you expect b.GetType() to
exhibit inside the DoIt method? It will return "C",
not "B".

However, the behavior you've described is what I'd expect
to occur, but that may only be because my vision is
skewed from modeling classes that way.

JER
-----Original Message-----
Here are three simple classes:

class A
{
public void DoIt(B b)
{
DoSomething(b);
}
public void DoSomething(B b)
{
}
public void DoSomething(C c)
{
}
}

class B
{
}

class C : B
{
}

Here is some simple code with line numbers:

1. A a = new A();
2. C c = new C();
3. a.DoSomething(c );
4. a.DoIt(c);

Here is my question:

Line 3 will cause the overloaded method A.DoSomething(C c) to be called, butline 4 causes A.DoSomething(B b) to be called. Why? That seems pretty wackif you ask me. Yes, I can see that the first method to be called is DoIt(Bb), which then apparently causes the "wrong" subsequent method to be called.But what is the underlying rationale for such behavior?

.

Nov 15 '05 #5
Steve M <st***@nospam.c om> wrote:
It is a drawback of C#.

Because overloaded method calls are determined at compile time rather than
runtime.

You ought to be able to designate overloaded methods as being "virtual" much
like overriden methods--so that the expected method is called.


I don't see it as a drawback - I see it as *mostly* an advantage:

o The method being known ahead of time makes for much faster code
o The code is easier to follow because you can tell from reading the
source which method will be called (admittedly with polymorphism)
o The language is simpler (describing which method would be used
with multiple overloads and multiple overrides would be a nightmare;
it's bad enough as it is when it comes to overloading)
o There's no room for ambiguity at runtime.

The last point is an important one, IMO: you could easily end up with
an actual parameter which satisfies two or more methods equally well
(eg if the formal parameter types are unrelated interfaces, and you
happen to get a reference to an object of a type which implements both
interfaces) - what would you do then? A program which has compiled
successfully shouldn't have to give up at runtime because it can't find
the right method!

I'm glad you're at least suggesting it to be optional, but I haven't
seen many times where I'd use this, and keeping the language simple and
easy to follow is a big plus in my view.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
> I don't see it as a drawback - I see it as *mostly* an advantage:
Sounds like, "It's not a bug, it's a feature!"

o The method being known ahead of time makes for much faster code

Then let's throw out the "virtual" keyword.

o The code is easier to follow because you can tell from reading the
source which method will be called (admittedly with polymorphism)

If we had any doubts before, surely now we should throw out "virtual".

o The language is simpler (describing which method would be used
with multiple overloads and multiple overrides would be a nightmare;
it's bad enough as it is when it comes to overloading)

You've convinced me to remove all overrides from my applications.

o There's no room for ambiguity at runtime.

Not anymore. Inheritance has officially been banned in my shop!
Long live C, Pascal, and COBOL!
Nov 15 '05 #7
Steve M <st***@nospam.c om> wrote:
I don't see it as a drawback - I see it as *mostly* an advantage:


Sounds like, "It's not a bug, it's a feature!"


Indeed. It's behaving the way it was designed to, and I'm glad it was
designed the way it was.
o The method being known ahead of time makes for much faster code


Then let's throw out the "virtual" keyword.


Speed was only one reason: I don't believe its utility is worth the
performance penalty, whereas the balance with virtual is entirely the
other way round.
o The code is easier to follow because you can tell from reading the
source which method will be called (admittedly with polymorphism)


If we had any doubts before, surely now we should throw out "virtual".


There's only one "dimension" in polymorphism - you don't have to worry
about the types of multiple things as you do with parameters, there's
only the type of the object being operated on.
o The language is simpler (describing which method would be used
with multiple overloads and multiple overrides would be a nightmare;
it's bad enough as it is when it comes to overloading)


You've convinced me to remove all overrides from my applications.


That's only because you appear to have decided not to really consider
my argument carefully.
o There's no room for ambiguity at runtime.


Not anymore. Inheritance has officially been banned in my shop!
Long live C, Pascal, and COBOL!


Where does polymorphism give room for ambiguity at runtime?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
> Where does polymorphism give room for ambiguity at runtime?

Actually, it doesn't. And I don't believe my suggestion for a more dynamic
overloading mechanism does either. If the compiler will balk at ambiguity in
its currently implementation, it surely could do so in a dynamic one as
well.
Nov 15 '05 #9
Steve M <st***@nospam.c om> wrote:
Where does polymorphism give room for ambiguity at runtime?


Actually, it doesn't. And I don't believe my suggestion for a more dynamic
overloading mechanism does either. If the compiler will balk at ambiguity in
its currently implementation, it surely could do so in a dynamic one as
well.


How, without loss of utility and at the expense of simplicity?
Consider:

interface IFoo
{
}

interface IBar
{
}

class X : IFoo, IBar
{
}

class Y
{
void Something(objec t o)
{
Overloaded(o);
}

void Overloaded(obje ct o)
{
}

void Overloaded(IFoo x)
{
}

void Overloaded(IBar y)
{
}
}

No problems at compile-time here - Overloaded(obje ct o) is called by
Something whatever the actual type of the object referred to by the
value of o is. Now consider what would happen in the system you're
suggesting, with dynamic overloading. What happens when you pass in a
reference to an instance of X? Both Overloaded(IFoo x) and
Overloaded(IBar y) are better than Overloaded(Obje ct o), but neither is
better than each other - so you have ambiguity. This can be seen at
compile-time in the current system if you try to change the parameter
of Something to X x instead of object o.

Now, the compiler could catch the potential ambiguity here, but that
makes the language even *more* complicated - and still for little gain.
It would also limit the usefulness of the virtual overloading in the
first place, as you'd never be able to specify disparate interfaces (or
a class and an interface that class doesn't implement) as overloaded
parameters for two different methods.

I stand by my view that the "cost" of this system in terms of
simplicity and efficiency outweighs the utility.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

17
4713
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
4
9320
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
14
1731
by: Klaus Löffelmann | last post by:
Hi, does anybody know, why the second contructor isn't called in this example? Are you able to reproduce this bug? Thanks Klaus Public Class Something
1
2156
by: Suresh Tri | last post by:
Hi, I was trying to overload concat operator ||(text,text) such a way that it behaves like Oracle. i.e. I want 'abc' || null to return 'abc' instead of null. I know that it is not the expected behaviour in postgres, but since I am migrating the database from oracle to postgres , I need this behaviour. But when I try to drop the existing || operator, I get the following
4
1862
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I never fully understood what overloading was good for. Could someone please provide me with some words that define overloading, what it is used for and some really basic examples.
10
2179
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
3
2138
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have two functions with the same name, this is an error - even when the functions take different parameters, and they would happily exist as overloaded functions if they were declared in the same class instead of being inherited.
8
5302
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
160
5827
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
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
8781
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
8638
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
7381
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
5662
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
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.