TITLE: What is multi-dispatching (multimethods double-dispatching)? My previous tip on multi-dispatching (AKA multimethods or double-dispatching) was obviously not sufficiently vague to snow everyone. Since several people have asked me what multidispatching is, I have included a definition below. If anyone knows of errors or deficiencies in this, please speak up. Single-Dispatching ------------------ C++ has support for single dispatching, also known as virtual method calls (polymorphism). With single dispatching, designers can call (dispatch) different methods depending upon the dynamic type of the target object. For example, class Shape { public: ... virtual void draw (); // does nothing by default }; class Circle : public Shape { public: ... void draw (); // renders a circle }; Client code takes advantage of the design via ... Shape* sp = someFunctionReturngingAShape (); sp->draw (); Multi-dispatching ----------------- Multidispatching is language/compiler support for dynamically choosing a method (or function) based upon the types of MORE than one object. This commonly occurs when parallel heirarchies exist. The need for MD occurs for any X::m(A&) where the method "m" needs to be different for different combinations of subclasses of X and A. These methods "m" are only indirectly related to either A or X. They really belong to a new type. This new type is an objectified instance of the RELATIONSHIP between Xs and As. An example (adapted from Icon Computing's "Advanced C++" tutorial notes) is in order. Consider class Vehicle; class ATV : public Vehicle; class Truck : public Vehicle; class Driver; class RegularDriver : public Driver; class ProDriver : public Driver; In the above classes, professional drivers are required to drive trucks. Regular drivers may not drive trucks. Either driver may drive ATVs. The method of interest is a register method. Registering drivers depends on the types of BOTH the driver and the vehicle (RD-ATV PD-ATV go down the hall to the left; PD-T to the right for extensive examinations; and RD-T are rejected as illegal). In fact, to make the problem simple, our register method will simply return 1 if the driver-vehicle pair is legal and 0 if not. The code below is a partial implementation of "double dispatching". class Vehicle { virtual int registerDriver (Driver& d) { return 0; } virtual int registerProDriver (ProDriver& d) { return registerDriver (d); } }; class Truck : public Vehicle { virtual int registerProDriver (ProDriver& d) { return 1; } } class Driver { virtual int register (Vehicle& v) { return v.registerDriver (this); } }; class ProDriver { virtual int register (Vehicle& v) { return v.registerProDriver (this); } }; Note that this is a workaround since C++ does not directly support multimethods. In C++ the identity (type) of the receiver of a virtual call is unambiguous. The strategy is to use virtual methods of the first object which will then use a virtual method call on the second object (double-dispatching, get it?). Weaknesses: o must enumerate all combinations o maintenance error prone o adding new classes requires large code changes o classes polluted with outside class information In my opinion, the weaknesses far outweigh any benefits. In fact, if you aren't grossed out by explicit type tests, the following is another approach int Register (Driver& d, Vehicle& v) { if (d->isA("ProDriver") && v->isA("Truck")) return 1; else return 0; }