-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisitorBefore.hpp
79 lines (62 loc) · 2.26 KB
/
VisitorBefore.hpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <iostream>
namespace visitor { namespace before {
class Color {
public:
virtual auto Count()->void = 0;
virtual auto Call()->void = 0;
static auto report_num()->void {
cout << "Reds " << s_num_red << ", Blus " << s_num_blu <<
'\n';
}
virtual ~Color() = default;
protected:
static int s_num_red, s_num_blu;
};
int Color::s_num_red = 0;
int Color::s_num_blu = 0;
class Red
: public Color {
public:
virtual auto Count()->void override {
++s_num_red;
}
virtual auto Call()->void override {
Eye();
}
static auto Eye()->void {
cout << "Red::eye\n";
}
~Red() override = default;
};
class Blu
: public Color {
public:
virtual auto Count()->void override {
++s_num_blu;
}
virtual auto Call()->void override {
Sky();
}
static auto Sky()->void {
cout << "Blu::sky\n";
}
~Blu() override = default;
};
inline auto Run()->void
{
Color *color_set[] =
{
// ReSharper disable CppNonReclaimedResourceAcquisition
new Red, new Blu, new Blu, new Red, new Red, nullptr
};
for (auto color:color_set) {
if (color) {
color->Count();
color->Call();
delete color;
}
}
Color::report_num();
}
} }