Re: Some thoughts on polymorphism
On May 3, 12:41 pm, pmouse <pmo...@cogeco.ca> wrote:
On May 2, 11:37 pm, Kirit S=E6lensminde <kirit.saelensmi...@gmail.com>
wrote:
On May 3, 12:51 am, faceman28...@yahoo.com wrote:
On May 1, 11:43 pm, chsal...@gmail.com wrote:
I've been programming in C++ for a little over 2 years, and I still
find myself wondering when I should use polymorphism.
The reality is polymorphism really doesn't show great value unless you
have a sufficent number number of related classes. Thus most academic
examples are pretty worthless.
In some ways it is only a minor point, but polymorphism isn't
inheritance, although inheritance is a form of polymorphism. There are
many other polymorphic constructs in C++.
std::list (and the rest of the STL containers) are polymorphic
classes. std::max is a polymorphic function as are the ones in
<algorithm>. Even the C operator + (which C++ inherits) is
polymorphic.
Well, not really, the stl containers uses generic programming
concepts, they work on the basis of "policies" rather than
inheritance, and they can't really be called "polymorphic".
That's a very odd definition of polymorphism that you're using - it's
also wrong.
What you're talking about is "inclusional polymorphism". C++ has this
form in common with languages like Java which are more strongly typed.
Most other OO languages (the so called dynamic languages) primarily
use another form of polymorphism called "operational polymorphism" or
more commonly "duck typing". C++ has a limited form of this that can
be used at compile time.
It is also possible to convert from operational to inclusional
polymorphism[1] and the requirements that a template puts on the types
it can be used with can be analysed through something called row
polymorphism.
[1] http://www.kirit.com/Walking%2C%20talking%20and%20quacking%20in%20Java
You are making a mistake common amongst C++ and Java programmers of
confusing inclusional polymorphism and polymorphism in general which
is exactly why I raised the point.
Note that pure functional languages are polymorphic but don't have any
class inheritance (not in the sense we mean in C++ anyway). Google for
"Haskell" or "ML" and "polymorphism" and you will notice the
discussion is about exactly the same forms that the STL uses.
K