加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

复杂的dynamic_cast在c

发布时间:2020-12-16 03:47:48 所属栏目:百科 来源:网络整理
导读:我在C中有以下情况: 抽象基类Abstract1和Abstract2.他们是无关的. 一个派生自Abstract1和Abstract2的Foo类 我在一个编译单位,我没有关于Foo类的信息(没有声明,没有定义).只有Abstract1和Abstract2是已知的. (实际上,Foo甚至在DLL中定义) 将dynamic_cast允许
我在C中有以下情况:

>抽象基类Abstract1和Abstract2.他们是无关的.
>一个派生自Abstract1和Abstract2的Foo类

我在一个编译单位,我没有关于Foo类的信息(没有声明,没有定义).只有Abstract1和Abstract2是已知的.
(实际上,Foo甚至在DLL中定义)

将dynamic_cast允许从Abstract1 *转换为Abstract2 *?这是标准吗?

解决方法

你所描述的是一种所谓的交叉角色.对于dynamic_cast< T(v),标准在[expr.dynamic.cast] / 8

If C is the class type to which T points or refers,the run-time
check logically executes as follows:

  • If,in the most derived object pointed (referred) to by v,v points (refers) to a public base class subobject of a C object [..]

  • Otherwise,if v points (refers) to a public base class subobject of the most derived object,and the type of the most derived object
    has a base class,of type C,that is unambiguous and public,the
    result points (refers) to the C subobject of the most derived
    object.

即使没有关于Foo在包含演员的翻译单元中的存在的信息,这将会工作.

你也应该看看this question.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读