C++ only supports single dynamic dispatch methods. Indeed, the following program:
#include <iostream>
struct Shape {
virtual void overlap(Shape* y) { std::cout << "Shape, Shape\n"; }
};
struct Circle: Shape {
void overlap(Shape* y) { std::cout << "Circle, Shape\n"; }
void overlap(Circle* y) { std::cout << "Circle, Circle\n"; }
};
void overlap(Shape* x, Shape* y) { std::cout << "Shape, Shape\n"; }
void overlap(Circle* x, Shape* y) { std::cout << "Circle, Shape\n"; }
void overlap(Circle* x, Circle* y) { std::cout << "Circle, Circle\n"; }
int main() {
Shape* x = new Circle();
Shape* y = new Circle();
x->overlap(y);
overlap(x, y);
return 0;
}
outputs:
Circle, Shape
Shape, Shape
If C++ supported multiple dynamic dispatch methods (i.e. with privileged access to a single parameter, the message receiver) and multiple dynamic dispatch functions (i.e. without privileged access to any parameters), the previous program would output:
Circle, Circle
Circle, Circle
Common Lisp, Dylan and Julia support multiple dynamic dispatch functions but do not support methods (single or multiple dynamic dispatch).
Would it be possible for a language to support multiple dynamic dispatch methods (instead of or in addition to multiple dynamic dispatch functions)?
I am asking this because supporting only multiple dynamic dispatch functions looks like a step backward to procedural programming. Encapsulation (implementation hiding behind an interface) is the cornerstone of object-oriented programming. Using functions in place of methods forces the exposure of the supposedly hidden states of objects to be able to manipulate them, because contrary to methods, functions do not have a privileged access to these states.