-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvisitor2.cpp
More file actions
51 lines (39 loc) · 739 Bytes
/
visitor2.cpp
File metadata and controls
51 lines (39 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "study.hpp"
struct D1;
struct D2;
struct BVisitor
{
virtual void visit(D1& d) = 0;
virtual void visit(D2& d) = 0;
};
struct B {
virtual void foo() = 0;
virtual void accept(BVisitor& bv) = 0;
};
template <typename Derived, typename Base, typename Visitor>
struct visitable : Base
{
virtual void accept(Visitor& v)
{
v.visit(static_cast<Derived&>(*this));
}
};
struct D1 : visitable<D1, B, BVisitor> { };
struct D2 : visitable<D1, B, BVisitor> { };
struct ShowVisitor : BVisitor
{
void visit(D1& d) { SHOW(); }
void visit(D2& d) { SHOW(); }
};
int main()
{
ShowVisitor sv;
BVisitor& bv = sv;
D1 d1;
D2 d2;
B& b1 = d1;
B& b2 = d2;
b1.accept(bv);
b2.accept(bv);
return 0;
}