回答編集履歴
1
a
test
CHANGED
@@ -3,3 +3,177 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
strike1217さんはおそらく型システムの話と実行時の話の区別がついていないように思われます。継承絡みのことはHogeAnimalLoverさん指摘の通り。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
> virtual基底クラス・・・virtual関数を1つでも持っているクラスが基本クラスになっているクラスのことですか?
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
違います。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
> 質問中に載せたコードは、何がおかしいのでしょうか? Never Succedd というエラーが表示されるやつです。ダウンキャストに成功しないのはなぜでしょう?
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
[https://wandbox.org/permlink/B8AO4UuUfybl3HoO](https://wandbox.org/permlink/B8AO4UuUfybl3HoO)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
prog.cc:29:61: warning: dynamic_cast of 'Base base_obj' to 'class Derived*' can never succeed
|
32
|
+
|
33
|
+
Derived *derived_obj_ptr = dynamic_cast<Derived *>(&base_obj);
|
34
|
+
|
35
|
+
^
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
これですかね?まずこれclangはなにもいいません。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
で、ちょっとコードをいじってみましょう。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```cpp
|
50
|
+
|
51
|
+
#include <iostream>
|
52
|
+
|
53
|
+
struct Base {
|
54
|
+
|
55
|
+
int member = 3489;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
void function(){
|
60
|
+
|
61
|
+
std::cout << "Base::funtion" << std::endl;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
virtual void func(){
|
68
|
+
|
69
|
+
std::cout << "Base::virtual_func" << std::endl;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
};
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
class Derived final : public Base {
|
78
|
+
|
79
|
+
public:
|
80
|
+
|
81
|
+
int Derived_member = 3829;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
void function(){
|
86
|
+
|
87
|
+
std::cout << "Derived::function" << std::endl;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
void func(){
|
94
|
+
|
95
|
+
std::cout << "Derived::virtual_func" << std::endl;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
};
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
Base base_obj;
|
106
|
+
|
107
|
+
Base* b_p1 = &base_obj;
|
108
|
+
|
109
|
+
Derived derived_obj;
|
110
|
+
|
111
|
+
Base* b_p2 = &derived_obj;
|
112
|
+
|
113
|
+
Derived *derived_obj_ptr1 = dynamic_cast<Derived *>(&base_obj);
|
114
|
+
|
115
|
+
Derived *derived_obj_ptr2 = dynamic_cast<Derived *>(b_p1);
|
116
|
+
|
117
|
+
Derived *derived_obj_ptr3 = dynamic_cast<Derived *>(new Base());
|
118
|
+
|
119
|
+
Derived *derived_obj_ptr4 = dynamic_cast<Derived *>(b_p2);
|
120
|
+
|
121
|
+
int main(){
|
122
|
+
|
123
|
+
std::cout
|
124
|
+
|
125
|
+
<< reinterpret_cast<void*>(&base_obj) << std::endl
|
126
|
+
|
127
|
+
<< reinterpret_cast<void*>(derived_obj_ptr1) << std::endl
|
128
|
+
|
129
|
+
<< reinterpret_cast<void*>(derived_obj_ptr2) << std::endl
|
130
|
+
|
131
|
+
<< reinterpret_cast<void*>(derived_obj_ptr3) << std::endl
|
132
|
+
|
133
|
+
<< reinterpret_cast<void*>(derived_obj_ptr4) << std::endl;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
[https://wandbox.org/permlink/SGYDJwnQ5vfoOxSO](https://wandbox.org/permlink/SGYDJwnQ5vfoOxSO)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
実行結果は
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
0x6012c0
|
152
|
+
|
153
|
+
0
|
154
|
+
|
155
|
+
0
|
156
|
+
|
157
|
+
0
|
158
|
+
|
159
|
+
0x6012e0
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
のようになりました。まあ当たり前ですよね。
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
静的にあきらかに失敗するとわかるdynamic_castの使用にはエラーや警告を出してくれるようです。
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
ref:
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
- [dynamic_cast の実装を読み解く - Qiita](https://qiita.com/msmania/items/77ccf8194384bdb3568f)
|
178
|
+
|
179
|
+
- [c++ - dynamic_cast<B *> (&a) gives a warning - Stack Overflow](https://stackoverflow.com/questions/18030262/dynamic-castb-a-gives-a-warning)
|