This is the first article in a series dedicated to multiple dispatch - an advanced abstraction technique available to programmers out-of-the-box in some languages, and implementable in others. This first post in the series presents the technique and explains the problem it intends to solve. It uses C++ as the presentation language because C++ does not support multiple dispatch directly, but can be used to implement it in various ways. Showing how multiple dispatch is implemented in a language that doesn't support it natively is important, in my opinion, as it lets us understand the issue on a deeper level. Follow-up articles will keep focusing on multiple dispatch using other programming languages : Part 2 will show how to implement multiple dispatch in Python; Part 3 will use Common Lisp, where multiple dispatch comes built-in as part of a large and powerful object-oriented system called CLOS; Part 4 will use Clojure, a more modern attempt at a Lisp, where multiple dispatch is also built-in, but works somewhat differently. Polymorphism, single dispatch, multiple dispatch There are many kinds of polymorphism in programming. The kind we're talking about here is runtime subtype-based polymorphism, where behavior is chosen dynamically based on the runtime types of objects. More specifically, multiple dispatch is all about the runtime types of more than one object. The best way to understand multiple dispatch is to first think about single dispatch. Single dispatch is what we usually refer to as "runtime polymorphism" in languages like C++ and Java . We have an object on which we call a method, and the actual method being called at runtime depends on the runtime type of the object. In C++ this is done with virtual functions: class Shape { public: virtual void ComputeArea() const = 0; }; class Rectangle : public Shape { public: virtual void ComputeArea() const { std::cout << "Rectangle: width times height\n"; } }; class Ellipse : public Shape { public: virtual void Compu...
First seen: 2025-09-11 00:13
Last seen: 2025-09-11 10:14