Re: Polymorphism vs. Overloading

jhallen@world.std.com (Joseph H Allen)
Tue, 1 Nov 1994 02:40:16 GMT

          From comp.compilers

Related articles
[21 earlier articles]
Re: Polymorphism vs. Overloading odersky@ira.uka.de (Martin Odersky) (1994-10-31)
Re: Polymorphism vs. Overloading bevan@cs.man.ac.uk (1994-10-27)
Re: Polymorphism vs. Overloading pjj@cs.man.ac.uk (1994-10-28)
Re: Polymorphism vs. Overloading jhf@c3serve.c3.lanl.gov (1994-10-28)
Re: Polymorphism vs. Overloading mmcg@bruce.cs.monash.edu.au (1994-10-29)
Re: Polymorphism vs. Overloading hbaker@netcom.com (1994-10-29)
Re: Polymorphism vs. Overloading jhallen@world.std.com (1994-11-01)
Re: Polymorphism vs. Overloading kanze@lts.sel.alcatel.de (kanze) (1994-11-01)
Re: Polymorphism vs. Overloading davidm@Rational.COM (1994-10-31)
Re: Polymorphism vs. Overloading bill@amber.ssd.csd.harris.com (1994-11-01)
Re: Polymorphism vs. Overloading drichter@pygmy.owlnet.rice.edu (1994-11-01)
Re: Polymorphism vs. Overloading bevan@cs.man.ac.uk (1994-11-02)
Re: Polymorphism vs. Overloading bimbart@CS.kuleuven.ac.be (Bart Demoen) (1994-11-02)
[3 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: jhallen@world.std.com (Joseph H Allen)
Keywords: polymorphism
Organization: The World Public Access UNIX, Brookline, MA
References: 94-10-144 94-10-169
Date: Tue, 1 Nov 1994 02:40:16 GMT

> The difference is purely syntactical. Calls to overloaded functions look,
> well, like function calls. Calls to polymorphic functions require a dot or
> '->' somewhere. Really, that's the only difference. Artificial semantic
> restrictions placed by certain languages aside, you can always move the
> identifier or address-expression from the left of the dot into the
> parenthasis as the first argument to generate an equivelent overloaded
> function call.


Norman Ramsey <norman@flaubert.bellcore.com> wrote:
>A polymorphic function accepts arguments of different types, but has
>the same semantics (and usually implementation) regardless of the
>types of the arguments.


I confused the issue by using incorrect termiology. I should have said
"member function" not "polymorphic function." The original poster was
interested in polymorphism in an environment which has overloading (i.e.
C++, simula, etc.), not polymorphic functions (for which your above
definition is correct). In C++, static polymorphism is done with
inheritance and overloading and dynamic polymorphism is done with virtual
functions.


[BTW: in which language is there more than one implementation created for
a polymorphic functions?]


Igor Chudov writes:
>1. They are just different and not even very similar.


No, they really are very similar. They accomplish the same thing: a
different function is called depending on the type of the arguments (if you
consider the left side of the '.' or '->' in a member function call an
argument).


>2. Polymorphism is dynamic, that is, the exact function is determined at
>runtime, when overloading is static and the exact function is determined
>at compile time.


Ah, but there is no reason overloaded functions have to work this way.
Suppose you have:


void foo(a *x,...) {...}


and


void foo(b *x,...) {...}


where a and b are classes and b inherits a. Then the following should work:


a q(...); // Instance of q
b r(...); // Instance of r
a *variable; // Pointer to a or its subtypes (or is that subclasses? :-)


variable= &a; foo(variable,...); // Call foo(a *x,...)
variable= &b; foo(variable,...); // Call foo(b *x,...)


Why doesn't this work? There's no syntactic reason that I can see. It's
just an arbitrary semantic restriction.


>3. In polymorphic mechanism, member function is chosen by the type of 'this'
>object, when in overloading the [member] function is chosen by the type of
>parameters.


'this' is really just an argument. It just appears on the left side of the
function name in the call (or message) and has an automatic PASCAL 'with'
statement implied. Also the 'polymorphic mechanism' you talk about only
works when the member function is declared to be 'virtual'.


>4. Overloading is just aliasing of different functions by one name.


But it could (and should) be equivalent to member functions. It's limited
for no good reason.


--
/* jhallen@world.std.com (192.74.137.5) */ /* Joseph H. Allen */
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.