Newsgroups: | comp.compilers |
From: | mmengel@cuuxb.ATT.COM (~XT6561110~Marc Mengel~C25~M27~6184~) |
Keywords: | polymorphism |
Organization: | AT&T National Technical Support Center |
References: | <9008310419.AA06194@karakorum.berkeley.edu> <PX.90Sep3180926@hal.fctunl.rccn.pt> |
Date: | 11 Sep 90 15:53:04 GMT |
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
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.