-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrtp.cpp
55 lines (42 loc) · 818 Bytes
/
crtp.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
#include <iostream>
template <typename Derived>
struct Base{
void interface(){
static_cast<Derived*>(this)->implementation();
}
void implementation()
{
std::cout << "Implementation Base" << std::endl;
}
};
struct Derived1: Base<Derived1>{
void implementation()
{
std::cout << "Implementation Derived1" << std::endl;
}
};
struct Derived2: Base<Derived2>
{
void implementation()
{
std::cout << "Implementation Derived2" << std::endl;
}
};
struct Derived3: Base<Derived3>{};
template <typename T>
void execute(T& base)
{
base.interface();
}
int main()
{
std::cout << std::endl;
Derived1 d1;
execute(d1);
Derived2 d2;
execute(d2);
Derived3 d3;
execute(d3);
std::cout << std::endl;
return 0;
}