C++ cannot convert from base A to derived type B via virtual base A

后端 未结 7 1474
不知归路
不知归路 2020-12-04 16:43

I have three classes:

class A {};

class B : virtual public A {};
class C : virtual public A {};

class D: public B, public C {};

Attemptin

7条回答
  •  清歌不尽
    2020-12-04 17:07

    You can't use static_cast in this situation because the compiler doesn't know the offset of B relative to A at compile time. The offset must be calculated at run-time based on the exact type of the most derived object. Therefore you must use dynamic_cast.

提交回复
热议问题