动态转换与接口 [英] dynamic cast with interfaces

查看:88
本文介绍了动态转换与接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类与implements 2接口和继承1类。所以,一般看起来像这样:

I have a class with implements 2 interfaces and inherits 1 class. So, generally it looks like this:

class T : public A, public IB, public IC {
};

代码中有一点,我有一个 IB * ,但真的可以使用 A * 。我希望一个动态的演员会喜欢这样:

There is one point in the code where I have an IB *, but could really use an A *. I was hoping that a dynamic cast would like this:

IB *b_ptr = new T; // it's really more complicated, but serves the example
A *a_ptr = dynamic_cast<A *>(b_ptr);

不幸的是,这不行。有正确的方法吗?还是我应该实施一个工作?我想到了 IB IC 几乎从继承A ,但IIRC上次我尝试有一些并发症,使其不可取。

unfortunately, this doesn't work. Is there a proper way to do this? Or should I implement a work around? I've thought about having both IB and IC inherit virtually from A, but IIRC last time I tried that there were some complications that made it undesirable.

任何想法?

EDIT :哦,这是一个插件API的一部分,所以不幸的是我没有直接访问 T 我需要 A * 。我的例子然后彼此相邻,但正如提到的,它更复杂。基本上我有2个共享库。 T T1 (我有一个 IB *

EDIT: oh yea, this is part of a plugin API, so unfortunately I don't have direct access to the T type where I need the A *. My example has then next to each other, but as mentioned, it's more complicated. Basically I have 2 shared libraries. T and T1 (where I have an IB *) are both classes which implement a plugin API and are internal to the shared libraries.

要澄清一下:这里是我的典型插件的一个更具体的例子(它们在不同的库中) :

To clarify: Here's a more specific example of my typical plugins (they are in separate libraries):

插件A:

class PluginA : public QObject, public PluginInterface, public OtherInterface {
};

插件B:

class PluginB : public QObject, public PluginInterface {
    // in here, I have a PluginInterface *, but really could use a QObject *
    // unfortunately, PluginB has absolutely no knowledge of the "PluginA" type
    // it just so happens that my PluginInterface * pointer points to an object of type
    // PluginA.
};

EDIT :我猜测问题是pluginA和pluginB在不同的共享库。也许rtti不跨越模块边界。我认为这可能是因为人们的例子似乎在我的测试中工作正常。具体来说,pluginB没有typeInfo PluginA,如果我做一个nm就可以了。这可能是问题的核心。如果是这种情况,我将不得不通过虚拟继承或虚拟 cast_to_qobject()函数在我的一个接口中工作。

EDIT: I have a guess that the issue is that pluginA and pluginB are in different shared libraries. Perhaps the rtti doesn't cross module boundaries. I think this might be the case because people's examples seem to work fine in my tests. Specifically, pluginB has no "typeinfo for PluginA" if I do an "nm" on it. This may be the core of the issue. If this is the case, I'll simply have to work around it by either virtual inheritance or a virtual cast_to_qobject() function in one of my interfaces.

推荐答案

我终于明白了, Daniel Paull 是正确的,应该允许侧向 dybnamic_cast 。我的问题是因为我的代码涉及共享库。 PluginA中的typeinfo在PluginB中不可用。我的解决方案是有效地添加 RTLD_NOW RTLD_GLOBAL 到我的加载过程

I finally figured it out, Daniel Paull was correct in that a "sideways dybnamic_cast" should be allowed. My problem was because my code is involving shared libraries. The typeinfo from PluginA was not available in PluginB. My solution was to effectively add RTLD_NOW and RTLD_GLOBAL to my load process

技术上是

loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);

因为我使用Qt的插件系统,但同样的区别。这些标志强制立即解析加载库中的所有符号,并使其他库可见。因此,使得typeinfo可用于需要它的每个人。这些标志到位后, dynamic_cast 工作正常。

because I'm using Qt's plugin system but same difference. These flags force all symbols from loaded libraries to be resolved immediately and be visible to other libraries. Therefore making the typeinfo available to everyone that needed it. The dynamic_cast worked as expected once these flags were in place.

这篇关于动态转换与接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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