January 2019
Intermediate to advanced
512 pages
14h 5m
English
The Acyclic Visitor pattern does not need a base class with the list of all visitable types. However, it has its own share of the boilerplate code. First of all, each visitable type needs the accept() member function, and it has more code than the similar function in the original Visitor pattern:
class Cat : public Pet { public: void accept(PetVisitor& v) override { if (CatVisitor* cv = dynamic_cast<CatVisitor*>(&v)) cv->visit(this); else { // Handle error assert(false); } }};
Assuming that the error handling is uniform, this function is repeated over and over for different types of visitors, each corresponding to its visitable type (such as CatVisitor here). Then there is the per-type Visitor class itself, for example: ...