Re: "Virtual functions allow polymorphism on a single argument" ?
On May 11, 12:07 am, desktop <f...@sss.com> wrote:
Alf P. Steinbach wrote:
* desktop:
This page:
http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html
start with the line: "Virtual functions allow polymorphism on a single
argument". What does that exactly mean?
I guess it has nothing to do with making multiple arguments in a
declaration like:
virtual void setId(int a, int b) {id = a+b;}
Right. The argument in question is the implicit this-pointer, the
object you're calling the member function on. And what it means is that
what member function implementation to call is selected based on the run
time type of that argument.
Ok so the argument in question is "obj" in this context:
obj.callMe()
where obj is the object that the member function "callMe()" is called upo=
n.
Yes. In C++ syntax. Conceptually, this can be mapped to
callMe( obj ), with polymorphism always occuring on the first
object (in C++).
Now consider something like "obj.callMe( arg )". Conceptually,
this would be "callMe( obj, arg )". In C++, polymorphism only
works on the first argument here. In other languages, the
actual function called can depend on the dynamic type of both
arguments, e.g. in CLOS: "(callMe obj arg)", the actual function
called can depend on the type of obj, the type of arg or both
(or neither).
As Alf pointed out, this can get a bit hairy: if both arguments
can have 10 different types, this means 100 different functions.
Which you have to write. And to add a new type, you practically
have to know all of the existing types, in order to add all of
the additional functions.
In practice, in C++, this can be implemented by calling a
virtual function on the first object, and having it call a
virtual function on the second. But with the constraint that
the base class has to know of the existance of all of the
derived types.
--
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