Re: Polymorphism and inheritance
On Sep 9, 3:36 am, tony_in_da...@yahoo.co.uk wrote:
On Sep 9, 5:26 am, Ali Karaali <ali...@gmail.com> wrote:
[...]
Anyway, exactly what polymorphism is is popularly disputed.
The C++ has a definition, which is valid for C++.
Some people use it to refer to virtual dispatch based run-time
polymorphism by default,
That's the way the standard uses it, and without qualification,
I think it's what most people would understand.
and explicitly say "compile-time polymorphism" when thinking
of templates, and would argue themselves blue in the face that
C++ doesn't have any other polymorphic mechanisms.
The only other "definition" I've seen is simply that a function
call may resolve to different functions, depending on the types
involved. Whether the "resolution" is at compile time
(templates and function overload resolution), link time
(different implementations of the same function, in different
object files) or runtime.
With so many computing "experts" floating around, polymorphism
has inevitably been redefined to mean whatever somebody meant
when they said it, but IMHO the best criteria is that it
allows the same piece of source code to generate different
code depending on the types to which the variable involved
resolve.
Which would exclude virtual function resolution. I've never
heard or seen that definition.
For example...
a = x + y
...is valid code whether a, x and y are ints or doubles. This
relies on compile-time polymorphism provided by the compiler
itself. A good test for polymorphic language features is can
they be used to write...
is_less_than(x, y)
...where x and y are not both locked in to one specific type.
C++ mechanisms that allow this include:
- preprocessor macros
- overloading
- virtual dispatch
- templates
And by providing different implementations in different object
files, the user choosing at link time. (And what about
providing different implementations in dynamically linked object
files:-)? Runtime polymorphism? Certainly not what most people
would understand by it.)
All these mechanisms are polymorphic.
Still, the test isn't perfect: you could write an
is_less_than(x, y) implementation using memcmp() and sizeof
that produced results that were meaningful only in the sense
that they were consistent (at least for types without
structure padding), and therefore useful for sorting/
searching, but it wouldn't actually be polymorphic because
there hasn't been type-driven code generation. That's more in
line with C- style generic programming ala the bsearch() or
qsort() functions common on UNIX platforms.
The point about polymorphism is that the implementation isn't
fixed by the function call itself.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34