Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
by deckar01 on Wednesday March 06, 2019 @10:07AM
(#19319401)
I don't understand why the class instance needs to accept the interaction. Why not just invoke the interaction directly?
Would this still be considered the visitor pattern or is the extra layer of indirection important?
[ Parent
| on HN ]
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
by fraffo on Wednesday March 06, 2019 @10:12AM
(#19319439)
Your code won't compile because interactor::interact is not defined on the base class.
That line is needed because the overload resolution is static.
So with the macro ACCEPTS, you build the code where the overload is resolved.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
by deckar01 on Wednesday March 06, 2019 @10:21AM
(#19319509)
I see. There is no interact signature that accepts IThingy, which is the whole point of this pattern. Thanks.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
by scroot on Thursday March 07, 2019 @12:07PM
(#19329751)
I don't know about the limitations of C++, but something like what you describe would happen in Smalltalk. Take for example a Canvas object and a bunch of different shape objects (square, circle, etc):
"Square class"
drawOn: aCanvas
aCanvas line: (some point) to: (some point).
"etc"
"Triangle class"
drawOn: aCanvas
aCanvas line: self leftVertex to: self rightVertex.
"etc"
"Canvas class"
drawShapes: aCollection
aCollection do: [ :shape |
shape drawOn: self ]