回答編集履歴

1

2019/03/12 09:15

投稿

yumetodo
yumetodo

スコア5852

test CHANGED
@@ -1,5 +1,89 @@
1
- https://wandbox.org/permlink/uVyNmg1EKLUZXExx
1
+ [https://wandbox.org/permlink/uVyNmg1EKLUZXExx](https://wandbox.org/permlink/uVyNmg1EKLUZXExx)
2
2
 
3
3
 
4
4
 
5
5
  継承のところで自身のクラスのtemplate実引数を書き忘れています。
6
+
7
+
8
+
9
+ ```cpp
10
+
11
+ #include <iostream>
12
+
13
+ // 親クラス
14
+
15
+ template <class Child, class SomeType>
16
+
17
+ struct Base
18
+
19
+ {
20
+
21
+ void interface()
22
+
23
+ {
24
+
25
+ static_cast<Child *>(this)->implementation();
26
+
27
+ }
28
+
29
+
30
+
31
+ protected:
32
+
33
+ SomeType hoge;
34
+
35
+ };
36
+
37
+
38
+
39
+ // 子クラス1
40
+
41
+ template <class SomeType>
42
+
43
+ struct Derived1 : Base<Derived1<SomeType>, SomeType> // Derived1<SomeType>に
44
+
45
+ {
46
+
47
+ void implementation()
48
+
49
+ {
50
+
51
+ std::cout << "Derived1" << std::endl;
52
+
53
+ }
54
+
55
+ };
56
+
57
+
58
+
59
+ // 子クラス2
60
+
61
+ template <class SomeType>
62
+
63
+ struct Derived2 : Base<Derived2<SomeType>, SomeType> // Derived2<SomeType>に
64
+
65
+ {
66
+
67
+ void implementation()
68
+
69
+ {
70
+
71
+ std::cout << "Derived2" << std::endl;
72
+
73
+ }
74
+
75
+ };
76
+
77
+
78
+
79
+ int main()
80
+
81
+ {
82
+
83
+ Derived1<int>().interface();
84
+
85
+ Derived2<int>().interface();
86
+
87
+ }
88
+
89
+ ```