Related articles |
---|
[24 earlier articles] |
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) |
Re: Polymorphism vs. Overloading monnier@di.epfl.ch (1994-11-01) |
Re: Polymorphism vs. Overloading nickb@harlequin.co.uk (1994-11-09) |
Re: Polymorphism vs. Overloading franka@europa.com (1994-11-09) |
Newsgroups: | comp.compilers |
From: | bill@amber.ssd.csd.harris.com (Bill Leonard) |
Keywords: | polymorphism |
Organization: | Harris Computer Systems, Ft. Lauderdale FL |
References: | 94-10-144 94-10-154 |
Date: | Tue, 1 Nov 1994 21:33:17 GMT |
Gabriela O. de Vivo <gdevivo@conicit.ve> wrote:
>Last week I was invited to join a Thesis (MsC) presentation.
>At some point a question raised about the exact difference between
>Polymorphism and Overloading.
jhallen@world.std.com (Joseph H Allen) writes:
> 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.
Hmm, well not quite. There is more to polymorphism than pure syntax. A
polymorphic function uses the *dynamic* (read "runtime") type of an object,
rather than its static type, to determine which function should be called.
Polymorphism only makes sense in an object-oriented framework, where one
type can be "derived" from, and add information to, another. (Don't
confuse this with Ada's derived types, which is purely an
information-hiding construct. In Ada, derived types cannot add any new
information to the base type values.)
In C++, for instance, you can declare a class (sort of like a struct) called
Base, and then declare another class called Derived that is derived from
Base. This means that any object (a variable or a dynamically-allocated
structure) of type Derived has all the properties of a Base, with perhaps
more stuff added. E.g.,
class Base {
int a;
int b;
};
class Derived : public Base {
float c;
};
I've left out lots of details here, but the idea is that a Derived object
contains two ints and a float; the two ints come from the Base part of
Derived. Anything you can do to a Base, you can do to a Derived, but you
might be able to do more with a Derived than you can with a Base.
Now, I can declare a pointer variable to be a "pointer to Base", but it
might in actuality point to either a Base object or a Derived object (or
maybe some other class derived from Base). Let's augment our Base and
Derived classes with a polymorphic function for illustration:
class Base {
public:
virtual int foo();
int a;
int b;
};
class Derived : public Base {
public:
virtual int foo();
float c;
};
This says there are two versions of function "foo": one for class Base, one
for class Derived. Now suppose we say:
Base * ptr = /* some expression yielding a Base pointer */
ptr->foo();
Which "foo" got called? It depends on what kind of object 'ptr' actually
points to, not on what type it is *declared* as. So at different times in
the execution of our program, 'ptr' may point to an object that is of type
Base, and other times it may point to an object that is of type Derived;
the call to "ptr->foo()" will invoke different foo functions in those
cases.
This is different from overloading, which relies on the *static* type of
the object, not its dynamic type. Overloading is not limited to
object-oriented languages. Neither is overloading limited to operators;
some languages allow overloading of operators, some allow overloading of
functions, and some allow both.
The key point about overloading is that several operations can be declared
as having the same "name", but taking different types of operands, and the
compiler makes the selection based on the *static* types of the operands.
With polymorphism, the selection is made at runtime, based on the *dynamic*
type of one or more operands. (Not many languages allow polymorphism based
on the types of multiple operands -- this is known as "multiple dispatch".)
Sorry for the long-winded answer.
--
Bill Leonard
Harris Computer Systems Corporation
2101 W. Cypress Creek Road
Fort Lauderdale, FL 33309
Bill.Leonard@mail.csd.harris.com
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.