Re: Defining polymorphism vs. overloading

daveg@near.cs.caltech.edu (Dave Gillespie)
3 Sep 90 02:58:00

          From comp.compilers

Related articles
Defining polymorphism vs. overloading oliver@karakorum.berkeley.edu (1990-08-30)
Re: Defining polymorphism vs. overloading burley@world.std.com (1990-09-01)
Re: Defining polymorphism vs. overloading wright@gefion.rice.edu (1990-09-02)
Re: Defining polymorphism vs. overloading compilers-sender@esegue.segue.boston.ma.us (1990-09-03)
Re: Defining polymorphism vs. overloading hrubin@l.cc.purdue.edu (1990-09-03)
Re: Defining polymorphism vs. overloading px@fctunl.rccn.pt (1990-09-03)
Re: Defining polymorphism vs. overloading plph@caen.engin.umich.edu (1990-09-03)
Re: Defining polymorphism vs. overloading daveg@near.cs.caltech.edu (1990-09-03)
Re: Defining polymorphism vs. overloading dmw9q@uvacs.cs.Virginia.EDU (1990-09-05)
Re: Defining polymorphism vs. overloading pase@orville.nas.nasa.gov (1990-09-06)
Re: Defining polymorphism vs. overloading tub!wg@relay.EU.net (1990-09-06)
Re: Defining polymorphism vs. overloading pase@orville.nas.nasa.gov (Douglas M. Pase) (1990-09-06)
Re: Defining polymorphism vs. overloading ok@goanna.cs.rmit.OZ.AU (1990-09-07)
Re: Defining polymorphism vs. overloading pardo@cs.washington.edu (1990-09-07)
[9 later articles]
| List of all articles for this month |
Newsgroups: comp.compilers
From: daveg@near.cs.caltech.edu (Dave Gillespie)
In-Reply-To: burley@world.std.com's message of 1 Sep 90 05:31:13 GMT
Keywords: polymorphism
Organization: California Institute of Technology
References: <9008310419.AA06194@karakorum.berkeley.edu> <BURLEY.90Sep1013113@world.std.com>
Date: 3 Sep 90 02:58:00

In article <9008310419.AA06194@karakorum.berkeley.edu> oliver@karakorum.berkeley.edu (Oliver Sharp) writes:


    o Overloading means using the same name (or symbol) to invoke different
          code depending on the types of the arguments (or operands)....


    o Polymorphism means using the same piece of code to operate on objects of
          different types. In LISP, cons is polymorphic because you can give it
          arguments of different types but it goes ahead and does its thing without
          worrying about it.


>>>>> On 1 Sep 90 05:31:13 GMT, burley@world.std.com (James C Burley) said:
> Conceptually, what is the difference?


Here's my interpretation. Suppose you have several kinds of linked
lists in C or C++. Each is a struct with "next" and "prev" pointers,
plus other stuff. You want to define some functions, like "list_length"
or "append_lists," that operate on lists of any type.


Overloading approach:


    Define several versions of each function, and call them all
    "list_length", using C++'s function overloading to get the compiler
    to choose the right function for each type of list.


Polymorphism approach:


    Define each struct with "next" and "prev" as the first and second
    elements, respectively. Write a single "list_length" which takes
    a "void *" which must point to a list of one of these kinds of
    structs. Inside the function casts this to a pointer to a
    standard struct with only "next" and "prev" fields. (I believe
    the ANSI standard guarantees this to work.)


The advantage of overloading is that each type's "list_length" function can
be customized. The advantage of polymorphism is that the whole definition of
"list_length" sits in one place.


But from the point of view of a user of "list_length", there is no difference
here. The language hides which type of implementation the writer of
"list_length" chose. (Although because C was not designed as a polymorphic
language, some of the implementation details of the polymorphic "list_length"
show through to the user because the structs must be specially arranged.)


The built-in "+" operator can be considered overloaded or polymorphic; the
distinction only matters if you are implementing the operator yourself.


-- Dave
--
Dave Gillespie
    256-80 Caltech Pasadena CA USA 91125
    daveg@csvax.cs.caltech.edu, ...!cit-vax!daveg
--


Post a followup to this message

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