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

后端 未结 7 1467
不知归路
不知归路 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:13

    $5.2.9/2- "An expression e can be explicitly converted to a type T using a static_cast of the form static_cast(e) if the declaration “T t(e);” is well-formed, for some invented temporary variable t (8.5)."

    In your code you are attempting static_cast with 'T = B*' and 'e = A*'

    Now 'B* t(A*)' is not well-formed in C++ (but 'A* t(B*)' is because 'A' is a virtual unambiguous and accessible base of 'B'. Therefore the code gives error.

    0 讨论(0)
提交回复
热议问题