Double dispatch/multimethods in C++

You missed the “double” part of the double dispatch.

The point of this pattern is to make sure that the right method of the processor is called – the method that accepts the right type. Since the processor is initially not aware of the type of the object that’s passed to it, you need the object to tell the processor what its type is.

In essence, each object needs a virtual processMe(Processor &p) method, and the processor calls it. The implementation of processMe calls p.processObject(this). But this time around, “this” has a known type! So instead of infinite recursion, you end up with the right proceessObject called

Leave a Comment