diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index c85088e..17cdfa6 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -96,22 +96,22 @@ For example, a dummy file-system interface might look like the following: ```cpp struct Filesystem { virtual void write(std::string_view filename, std::span data) = 0; - virtual std::vector read(std::string_view filename) = 0; + virtual std::vector read(std::string_view filename) = 0; virtual void delete(std::string_view filename) = 0; }; ``` You can then write `PosixFilesystem` which makes use of the POSIX API and interact with actual on-disk data, `MockFilesystem` which only works in-memory -and can be used for testing, etc... +and can be used for tests only, etc... -## Double dispatch through the Visitor pattern +## Double dispatch -Sometimes single dispatch is not enough, such as in the collision example at the -beginning of this article. In cases where a computation depends on the dynamic -type of *two* of its values, we can make use of double-dispatch by leveraging -the Visitor design pattern. This is done by calling a virtual method on the -first value, which itself will call a virtual method on the second value. +Sometimes single dispatch is not enough, such as in the collision example at +the beginning of this article. In cases where a computation depends on the +dynamic type of *two* of its values, we can make use of double-dispatch by +calling a virtual method on the first value, which will call a virtual method +on the second value. Here's a commentated example: @@ -161,8 +161,8 @@ void collide(SpaceObject& first, SpaceObject& second) { }; int main() { - auto asteroid = std::make_unique(); - auto spaceship = std::make_unique(); + std::unique_ptr asteroid = std::make_unique(); + std::unique_ptr spaceship = std::make_unique(); collide(*asteroid, *spaceship); // Calls in order: