473,385 Members | 1,351 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,385 software developers and data experts.

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 1634
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.com>
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**************@TK2MSFTNGP09.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.com> 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.com>
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.com> 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.com>
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.com> 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(object o)
{
Overloaded(o);
}

void Overloaded(object o)
{
}

void Overloaded(IFoo x)
{
}

void Overloaded(IBar y)
{
}
}

No problems at compile-time here - Overloaded(object 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(Object 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.com>
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
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...
4
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
14
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
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 ...
4
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...
10
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
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...
8
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....
160
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.