I'm trying to implement the visitor pattern inside of another class. MWE:
struct super
{
struct base
{
virtual void accept(struct visitor& v);
virtual ~base() {}
};
struct visitor
{
virtual void visit(base& b);
virtual ~visitor() {}
};
struct special : public base
{
void accept(visitor& v) override { v.visit(*this); }
};
};
int main() {}
This complains that special::accept is actually not overriding anything. I guess this is because of struct visitor being different to visitor.
Swapping the position of base and visitor (and moving the forward declaration to visitor::visit) vanishes this error (but then says that the argument in v.visit(*this) would not match).
Is it possible to implement the visitor pattern inside another class? Why does my forward declaration not work?