473,503 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Polymorphism; simple question

Which version of foo() is bar() going to call? I'm not seeing what I
expected to see.

class A{}
class B : A{}
class C
{
public void foo(A a){}
public void foo(B b){}
}

some function bar()
{
C c = new C();
A obj = new B();

c.foo(obj);
}
Nov 16 '05 #1
3 903
David Vestal <so****************@mailcity.com> wrote:
Which version of foo() is bar() going to call? I'm not seeing what I
expected to see.

class A{}
class B : A{}
class C
{
public void foo(A a){}
public void foo(B b){}
}

some function bar()
{
C c = new C();
A obj = new B();

c.foo(obj);
}


I'd expect foo(A a) to be called. The method signature is determined at
compile time - and at that stage, the compiler only knows that the obj
is an "A" reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Just for fun...

using System;

namespace FooFooFoo

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

C c = new C();

A obj = new B();

c.foo(obj);

}

}

public class A {}

public class B : A {}

public class C

{

public void foo(A a)

{

System.Diagnostics.Debug.WriteLine("C:foo(A) Has been called");

}

public void foo(B b)

{

System.Diagnostics.Debug.WriteLine("C:foo(B) Has been called");

}

}

}
Nov 16 '05 #3
Hi David,
Which version of foo() is bar() going to call? I'm not seeing what I
expected to see.

class A{}
class B : A{}
class C
{
// i bet on this call:
public void foo(A a){}
public void foo(B b){}
// add this:
public void foo(object o) {};
} some function bar()
{
C c = new C();
A obj = new B();

c.foo(obj);
// add this:
c.foo((object) new B(); }


Compiler(or whatever it name) takes method with proper parameter
types. So if your object of class B is cast to A or object
then its better to call method with casted types.

Other thing is overriding methods of subclass...

Greetings

Marcin
Nov 16 '05 #4

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

Similar topics

37
2789
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
3
3659
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
4
4409
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
3
7420
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
10
1845
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long...
13
3215
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
18
3833
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
13
1980
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
11
2945
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
12
2507
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
0
7287
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
7349
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
5594
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,...
1
5022
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...
0
3177
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...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
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
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.