Double dispatch/multimethods in C++

前端 未结 4 1503
无人及你
无人及你 2020-12-03 12:21

I have a question on C++ double dispatch. In the code below, I want the results from the second set to match the results from the first set.

I don\'t know the actua

4条回答
  •  悲哀的现实
    2020-12-03 12:27

    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

提交回复
热议问题