C ++不能通过虚拟基本A从基本A转换为派生类型B. [英] C++ cannot convert from base A to derived type B via virtual base A

查看:171
本文介绍了C ++不能通过虚拟基本A从基本A转换为派生类型B.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个类:

  class A {}; 

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

class D:public B,public C {};

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

 无法通过虚拟基础A从基础A转换为派生类型B 


解决方案

为了理解你需要在对象模型中潜入的转换系统。



<一个简单的层次模型的经典表示是遏制:如果 B 源自 A ,则 B 对象将事实上包含一个 A 子对象及其自己的属性。



使用这个模型,downcasting是一个简单的指针操作,通过在编译时已知的偏移量,它取决于 B 的内存布局。



这是 static_cast :静态转换被称为静态,因为对于转换所需的计算是在编译时完成的,无论是指针运算还是转换*)。



但是,当 virtual 继承时,事情往往变得更加困难。主要的问题是使用 virtual 继承所有子类共享子对象的同一个实例。为了这样做, B 将有一个指向 A 的指针,而不是 A 正确,并且 A 基类对象将在 B 之外进行实例化。 / p>

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



每当有一个运行时类型依赖,你需要RTTI(运行时类型信息),使用RTTI的转换是 dynamic_cast 的工作。



总结:




  • 编译时下载: static_cast

  • 运行时下载: dynamic_cast



其他两个也是编译时强制转换,但它们是如此具体,很容易记住它们是什么...和他们是臭的,所以最好不要使用它们在所有反正。



(*)正如@curiousguy在注释中所指出的,这只适用于downcasting。一个 static_cast 允许向上转换,无论虚拟或简单的继承,虽然那么转换也是不必要的。


I have three classes:

class A {};

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

class D: public B, public C {};

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 in the object model.

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

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

This is what static_cast do: 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 (*).

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 a same instance of the subobject. In order to do that, B will have a pointer to a A, instead of a 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.

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.

In summary:

  • compile-time downcast: static_cast
  • run-time downcast: 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.

(*) 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.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆