1. <i id='1nX2H'><tr id='1nX2H'><dt id='1nX2H'><q id='1nX2H'><span id='1nX2H'><b id='1nX2H'><form id='1nX2H'><ins id='1nX2H'></ins><ul id='1nX2H'></ul><sub id='1nX2H'></sub></form><legend id='1nX2H'></legend><bdo id='1nX2H'><pre id='1nX2H'><center id='1nX2H'></center></pre></bdo></b><th id='1nX2H'></th></span></q></dt></tr></i><div id='1nX2H'><tfoot id='1nX2H'></tfoot><dl id='1nX2H'><fieldset id='1nX2H'></fieldset></dl></div>
      2. <tfoot id='1nX2H'></tfoot>

          <bdo id='1nX2H'></bdo><ul id='1nX2H'></ul>
        <legend id='1nX2H'><style id='1nX2H'><dir id='1nX2H'><q id='1nX2H'></q></dir></style></legend>

        <small id='1nX2H'></small><noframes id='1nX2H'>

        C++ 不能通过虚基 A 从基 A 转换为派生类型 B

        时间:2023-09-26
      3. <i id='6dqof'><tr id='6dqof'><dt id='6dqof'><q id='6dqof'><span id='6dqof'><b id='6dqof'><form id='6dqof'><ins id='6dqof'></ins><ul id='6dqof'></ul><sub id='6dqof'></sub></form><legend id='6dqof'></legend><bdo id='6dqof'><pre id='6dqof'><center id='6dqof'></center></pre></bdo></b><th id='6dqof'></th></span></q></dt></tr></i><div id='6dqof'><tfoot id='6dqof'></tfoot><dl id='6dqof'><fieldset id='6dqof'></fieldset></dl></div>

        <small id='6dqof'></small><noframes id='6dqof'>

          <tbody id='6dqof'></tbody>

            <bdo id='6dqof'></bdo><ul id='6dqof'></ul>

          • <legend id='6dqof'><style id='6dqof'><dir id='6dqof'><q id='6dqof'></q></dir></style></legend>

                • <tfoot id='6dqof'></tfoot>
                  本文介绍了C++ 不能通过虚基 A 从基 A 转换为派生类型 B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三个班级:

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

                  尝试从 A* 静态转换到 B* 我收到以下错误:

                  Attempting a static cast from A* to B* I get the below error:

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

                  推荐答案

                  为了理解强制转换系统,您需要深入研究对象模型.

                  In order to understand the cast system, you need to dive into the object model.

                  简单层次模型的经典表示是包含:如果 B 派生自 A 那么 B 对象实际上将包含一个 A 子对象以及它自己的属性.

                  The classic representation of a simple hierarchy model is containment: if B derives from A then the B object will, in fact, contain an A subobject alongside its own attributes.

                  使用此模型,向下转换是通过编译时已知的偏移量进行简单的指针操作,这取决于 B 的内存布局.

                  With this model downcasting is a simple pointer manipulation by an offset known at compilation time, which depends on the memory layout of B.

                  这就是 static_cast 的作用:静态转换被称为静态转换,因为转换所需的计算是在编译时完成的,无论是指针算术还是转换 (*).

                  This is what static_cast does: a static cast is dubbed static because the computation of what is necessary for the cast is done at compile-time, be it pointer arithmetic or conversions (*).

                  然而,当 virtual 继承开始时,事情往往变得有点困难.主要问题是使用 virtual 继承所有子类共享子对象的相同实例.为了做到这一点,B 将有一个指向 A 的指针,而不是一个 A 本身,而 Acode> 基类对象将在 B 之外实例化.

                  However, when virtual inheritance kicks in, things tend to become a bit more difficult. The main issue is that with virtual inheritance all subclasses share the same instance of the subobject. In order to do that, B will have a pointer to an A, instead of an A proper, and the A base class object will be instantiated outside of B.

                  因此,在编译时不可能推导出必要的指针算法:这取决于对象的运行时类型.

                  Therefore, it's impossible at compilation time to be able to deduce the necessary pointer arithmetic: it depends on the runtime type of the object.

                  每当有运行时类型依赖项时,您都需要 RTTI(运行时类型信息),而利用 RTTI 进行强制转换是 dynamic_cast 的工作.

                  Whenever there is a runtime type dependency, you need RTTI (RunTime Type Information), and making use of RTTI for casts is the job of dynamic_cast.

                  总结:

                  • 编译时向下转换:static_cast
                  • 运行时向下转换:dynamic_cast

                  另外两个也是编译时强制转换,但它们非常具体,以至于很容易记住它们的用途……而且它们很臭,所以无论如何最好不要使用它们.

                  The other two are also compile-time casts, but they are so specific that it's easy to remember what they are for... and they are smelly, so better not use them at all anyway.

                  (*) 正如@curiousguy 在评论中所指出的,这仅适用于向下转换.static_cast 允许向上转换,而不管是虚拟继承还是简单继承,尽管这样转换也是不必要的.

                  (*) As noted by @curiousguy in the comments, this only holds for downcasting. A static_cast allows upcasting regardless of virtual or simple inheritance, though then the cast is also unnecessary.

                  这篇关于C++ 不能通过虚基 A 从基 A 转换为派生类型 B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 什么时候我们应该更喜欢使用两个链接的 static_cast 而不是 reinterpret_cast 下一篇:转换指针类型的正确方法

                  相关文章

                    <bdo id='Osqeb'></bdo><ul id='Osqeb'></ul>

                • <i id='Osqeb'><tr id='Osqeb'><dt id='Osqeb'><q id='Osqeb'><span id='Osqeb'><b id='Osqeb'><form id='Osqeb'><ins id='Osqeb'></ins><ul id='Osqeb'></ul><sub id='Osqeb'></sub></form><legend id='Osqeb'></legend><bdo id='Osqeb'><pre id='Osqeb'><center id='Osqeb'></center></pre></bdo></b><th id='Osqeb'></th></span></q></dt></tr></i><div id='Osqeb'><tfoot id='Osqeb'></tfoot><dl id='Osqeb'><fieldset id='Osqeb'></fieldset></dl></div>

                  1. <legend id='Osqeb'><style id='Osqeb'><dir id='Osqeb'><q id='Osqeb'></q></dir></style></legend>
                    1. <small id='Osqeb'></small><noframes id='Osqeb'>

                      <tfoot id='Osqeb'></tfoot>