Re: "Virtual functions allow polymorphism on a single argument" ?
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 upon.
Since obj can be an instance of B,C or D (if they are all descendants
from a base class A) it is first at runtime it is decided which (B, C or
D) "callme()" function will be run.
Polymorphism on two or more arguments is difficult because the number of
possible function implementations is then the product of the number of
possible classes for each argument.
But how can there be more than one object that a function is called
upon? As I see it there can only be one (like obj) but it might differ
at runtime which type it is.
One useful technique is known as double dispatch; look it up.
I am currently reading this pattern but need to understand what they
mean with polymorphism with one or two arguments.