c++ - Virtual visitor -
i have hierarchy of classes (base
, daugthers a
, b
, c
, on). hierarchy tree not finite. it’s possible visit base*
using well-known visitor pattern in order dynamically adapt dynamic type of base*
. visitor has type, basevisitor
. this:
struct basevisitor; struct base { virtual void accept(basevisitor &v) = 0; }; struct : base { int a; a(int x) : a(x) {} ~a(void) = default; void accept(basevisitor &v) override { v->visit(*this); } }; struct b : base { float b; b(void) : b(314.f) {} ~b(void) = default; void accept(basevisitor &v) override { v->visit(*this); } }; struct basevisitor { void visit(base &) {} void visit(a &a) { std::cout << "just visited a; a=" << a.a << std::endl; } void visit(b &b) { std::cout << "just visited b; b=" << b.b << std::endl; } };
now, i’d make basevisitor
abstract class in order inherits new visitors. issue comes when want define abstract class:
struct abasevisitor { virtual void visit(base &) = 0; /* huh? */ };
do have idea?
Comments
Post a Comment