-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_nested.cpp
86 lines (64 loc) · 1.82 KB
/
test_nested.cpp
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
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
//abstract class definition
struct Move {
virtual void print_move() const =0;
};
typedef std::vector<Move*> Moves;
struct Board {
typedef ::Move Move;
virtual void print_board() const =0;
virtual Moves get_moves() const=0;
Move *move{};
};
typedef std::vector<Board*> Boards;
//class implementation
struct MoveA: public Move {
void print_move() const override { std::cout<<"move_a"<<std::endl; }
};
struct BoardA: public Board {
typedef MoveA Move;
BoardA() : Board() { this->move=new Move(); }
~BoardA() { delete this->move; }
void print_board() const override { std::cout<<"board_a"<<std::endl; }
Moves get_moves() const override {
Moves moves;
moves.push_back(this->move);
moves.push_back(this->move);
return moves;
}
};
struct BoardB: public Board {
struct Move: public ::Move {
void print_move() const override { std::cout<<"move_b"<<std::endl; }
};
BoardB() : Board() { this->move=new Move(); }
~BoardB() { delete this->move; }
void print_board() const override { std::cout<<"board_b"<<std::endl; }
Moves get_moves() const override {
Moves moves;
moves.push_back(this->move);
moves.push_back(this->move);
moves.push_back(this->move);
return moves;
}
};
//do the stuff
int main(int argc, char *argv[]) {
Boards boards;
boards.push_back(new BoardA());
boards.push_back(new BoardB());
for (Boards::const_iterator board_iter=boards.begin(); board_iter!=boards.end(); board_iter++) {
const Board *board=*board_iter;
board->print_board();
Moves moves=board->get_moves();
for (Moves::const_iterator move_iter=moves.begin(); move_iter!=moves.end(); move_iter++) {
const Board::Move *move=*move_iter;
move->print_move();
}
}
for (auto iter=boards.begin(); iter<boards.end(); iter++) {
delete *iter;
}
return 0;
}