I have a class that should call the visitor method for each member variable. Something like that:
class A{ int a, b, c; public: void accept(Visitor &visitor){ visitor.visit(a); visitor.visit(b); visitor.visit(c); } };
How can I get the void accept() const method with the same code without duplicating the code?
The obvious duplication solution is to add a method:
void accept(Visitor &visitor) const { visitor.visit(a); visitor.visit(b); visitor.visit(c); }
This method has exactly the meaning that I want, but I would like to avoid code duplication. The reason to have both methods is the ability to read variables by the visitor of the βreadβ and to have the accept method beautifully const . Then the non-constant accept can be used to "record / update" visitors.
Juraj blaho
source share