-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdouble_dispatch1.cpp
97 lines (84 loc) · 2.77 KB
/
double_dispatch1.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
87
88
89
90
91
92
93
94
95
96
97
// Double dispatching via virtual functions
#include <iostream>
#include <memory>
#include <string>
class Cat;
class Bird;
class Dog;
struct IAnimal {
virtual std::string name() const = 0;
virtual void play(const IAnimal&) const = 0;
virtual void play(const Cat&) const = 0;
virtual void play(const Bird&) const = 0;
virtual void play(const Dog&) const = 0;
virtual ~IAnimal() = default;
};
class Cat : public IAnimal {
public:
std::string name() const override { return "Cat"; }
void play(const IAnimal& animal) const override {
animal.play(*this); // double dispatching
}
void play(const Cat&) const override;
void play(const Dog&) const override;
void play(const Bird&) const override;
};
class Dog : public IAnimal {
public:
std::string name() const override { return "Dog"; }
void play(const IAnimal& animal) const override {
animal.play(*this); // double dispatching
}
void play(const Cat&) const override;
void play(const Dog&) const override;
void play(const Bird&) const override;
};
class Bird : public IAnimal {
public:
std::string name() const override { return "Bird"; }
void play(const IAnimal& animal) const override {
animal.play(*this); // double dispatching
}
void play(const Cat&) const override;
void play(const Dog&) const override;
void play(const Bird&) const override;
};
void Cat::play(const Cat& cat) const {
std::cout << name() << " plays with " << cat.name() << '\n';
}
void Cat::play(const Bird& bird) const {
std::cout << name() << " plays with " << bird.name() << '\n';
}
void Cat::play(const Dog& dog) const {
std::cout << name() << " plays with " << dog.name() << '\n';
}
void Dog::play(const Cat& cat) const {
std::cout << name() << " plays with " << cat.name() << '\n';
}
void Dog::play(const Bird& bird) const {
std::cout << name() << " plays with " << bird.name() << '\n';
}
void Dog::play(const Dog& dog) const {
std::cout << name() << " plays with " << dog.name() << '\n';
}
void Bird::play(const Cat& cat) const {
std::cout << name() << " plays with " << cat.name() << '\n';
}
void Bird::play(const Bird& bird) const {
std::cout << name() << " plays with " << bird.name() << '\n';
}
void Bird::play(const Dog& dog) const {
std::cout << name() << " plays with " << dog.name() << '\n';
}
void play(const IAnimal& first, const IAnimal& second) { second.play(first); }
int main() {
std::unique_ptr<IAnimal> upCat{std::make_unique<Cat>()};
std::unique_ptr<IAnimal> upDog{std::make_unique<Dog>()};
std::unique_ptr<IAnimal> upBird{std::make_unique<Bird>()};
play(*upCat, *upDog);
play(*upCat, *upBird);
play(*upDog, *upBird);
play(*upDog, *upCat);
play(*upBird, *upCat);
play(*upBird, *upDog);
}