Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!spdcc!ima!esegue!compilers-sender From: daveg@near.cs.caltech.edu (Dave Gillespie) Newsgroups: comp.compilers Subject: Re: Defining polymorphism vs. overloading Keywords: polymorphism Message-ID: Date: 3 Sep 90 01:58:00 GMT References: <9008310419.AA06194@karakorum.berkeley.edu> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: daveg@near.cs.caltech.edu (Dave Gillespie) Organization: California Institute of Technology Lines: 54 Approved: compilers@esegue.segue.boston.ma.us In-Reply-To: burley@world.std.com's message of 1 Sep 90 05:31:13 GMT 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 -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.