From bb48eaea3418a1b819b7f187039a3563a42e5ba9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 16:03:25 +0100 Subject: [PATCH] posts: multiple-dispatch: add visitor downside --- .../posts/2022-11-02-multiple-dispatch-in-c++/index.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/content/posts/2022-11-02-multiple-dispatch-in-c++/index.md b/content/posts/2022-11-02-multiple-dispatch-in-c++/index.md index e9eb21b..928f5f9 100644 --- a/content/posts/2022-11-02-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-11-02-multiple-dispatch-in-c++/index.md @@ -185,6 +185,16 @@ e.g: compilers, where the AST class hierarchy represents the data *only*, and all compiler stages and optimization passes are programmed by a series of visitors. +One downside of this approach is that if you want to add `SpaceStation` as +a sub-class of `SpaceObject`, and handle its collisions with other +`SpaceObject`s, you need to: + +* Implement all `collide_with` methods for this new class. +* Add a new virtual method `collide_with(SpaceStation&)` and implement it on + every sub-class. + +This can be inconvenient if your class hierarchy changes often. + ## Multiple dispatch on a closed class hierarchy When even double dispatch is not enough, there is a way to do multiple dispatch