According to Wikipedia, Multiple Dispatch is when
... a function or method can be dynamically dispatched based on the run time (dynamic) type of more than one of its arguments.
However, in C++ I can override a function in the derived class:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void method(int a) { cout << a; }
virtual void method(int a, int b) { cout << a + b; }
};
class Derived: public Base
{
void method(int a) { cout << a * 10 << endl; }
void method(int a, int b) { cout << (a + b) * 10 <<endl; }
};
int main()
{
Base* derived = new Derived;
derived->method(1);
derived->method(1, 2);
return 0;
}
Here the method
is being bound at runtime (because I am using virtual
) and the particular method is selected based on the input parameters - how is this any different from the Wikipedia description of multiple dispatch?
Secondary question: What is the advantage (if any) to languages which support multiple dispatch if this combination of polymorphism and method overloading exists in C++?