Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!think.com!mintaka!spdcc!esegue!compilers-sender From: mmengel@cuuxb.ATT.COM (~XT6561110~Marc Mengel~C25~M27~6184~) Newsgroups: comp.compilers Subject: Re: Defining polymorphism vs. overloading Keywords: polymorphism Message-ID: <5168@cuuxb.ATT.COM> Date: 11 Sep 90 15:53:04 GMT References: <9008310419.AA06194@karakorum.berkeley.edu> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: mmengel@cuuxb.ATT.COM (~XT6561110~Marc Mengel~C25~M27~6184~) Organization: AT&T National Technical Support Center Lines: 42 Approved: compilers@esegue.segue.boston.ma.us Okay. Time to settle this once and for all :-) Polymorphism (poly == many,morph == type) i.e. many-type-ism; is one operation that works on all (or at least many) types. Overloading is having the same token represent different operations. I am using "operation" here in a general sense; certainly integer and floating point addition are mechanically different in detail, yet for the "same" values (i.e. 5.0 + 5.0 and 5 + 5) an addition operator will yeild the "same" result (i.e. 10.0 and 10), floating point and integer addition are then the "same" operation. So if you're going to be polymorphic, you need to define what the operation is, and pick one that makes sense accross the set of types you are dealing with. You could for example define "<=" as a partial order on basically any type and still have a consistent operator which a sorting algorithm would work on for example. Overloading is when the same token stands for different operations altogether (i.e. "<<" being used for both bit shifts and standard I.O. in C++). This is (IMHO) a Bad Thing. Overloading (as per my definition anyway :-)) is something that makes code difficult if not impossible to read, especially when pushed to the limit. Polymorphism, on the other hand makes code easier to read, as it gets rid of things like having both a "div" and "/" operator in Pascal. [Incidentally, to soundly thump the dead horse of subscription in C, please remember that "a[b]" in C is eqivalent to "*(a+b)" and that it is the "+" operator on pointers and integers that is polymorphic, and always yeilds the pointer address plus the integer times the size of the object the pointer points to.] -- Marc Mengel mmengel@cuuxb.att.com attmail!mmengel ...!{lll-crg|att}!cuuxb!mmengel -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.