Eva*_*ran 7 c++ dynamic-cast casting
我有一个带有implements 2接口的类,并继承了1个类.所以,通常它看起来像这样:
class T : public A, public IB, public IC {
};
Run Code Online (Sandbox Code Playgroud)
代码中有一点我有一个IB *,但可以真正使用A *.我希望动态演员喜欢这样:
IB *b_ptr = new T; // it's really more complicated, but serves the example
A *a_ptr = dynamic_cast<A *>(b_ptr);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.有没有正确的方法来做到这一点?或者我应该实施一个解决方案?我已经考虑过这两个IB并且IC实际上是从遗传中继承的A,但是我上次尝试过IIRC时,有一些并发症使它变得不可取.
有什么想法吗?
编辑:哦,是的,这是一个插件API的一部分,所以不幸的是我没有直接访问T我需要的类型A *.我的例子彼此相邻,但如上所述,它更复杂.基本上我有2个共享库.T和T1(我有一个IB *)都是实现插件API的类,并且是共享库的内部.
澄清:这是我的典型插件的一个更具体的例子(它们在不同的库中):
插件A:
class PluginA : public QObject, public PluginInterface, public OtherInterface {
};
Run Code Online (Sandbox Code Playgroud)
插件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.
};
Run Code Online (Sandbox Code Playgroud)
编辑:我猜测问题是pluginA和pluginB在不同的共享库中.也许rtti不跨越模块边界.我认为情况可能就是这样,因为人们的例子似乎在我的测试中运作良好.具体来说,如果我在其上执行"nm",则pluginB没有"PluginA的typeinfo".这可能是问题的核心.如果是这种情况,我只需通过虚拟继承或cast_to_qobject()其中一个接口中的虚函数来解决它.
每个类至少有一个虚拟方法吗?如果没有,那就是你的问题.向每个类添加虚拟析构函数应该可以解决这个问题.
以下为我愉快地工作:
class IC
{
public:
virtual ~IC() {}
};
class IB
{
public:
virtual ~IB() {}
};
class A
{
public:
virtual ~A() {}
void foo() { /* stick a breakpoint here to confirm that this is called */ }
};
class T : public A, public IB, public IC
{
public:
virtual ~T() {}
};
int main(void)
{
IB *b_ptr = new T;
A *a_ptr = dynamic_cast<A *>(b_ptr);
a_ptr->foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
在所有新信息和异常行为(您的代码应该正常工作!)之后,以下是否有帮助?我引入了一个名为IObject的接口,并使用虚拟继承来确保该基类只有一个副本.你现在可以转向IObject然后转向A吗?
class IObject
{
public:
virtual ~IObject() {}
};
class IC : virtual public IObject
{
public:
virtual ~IC() {}
};
class IB : virtual public IObject
{
public:
virtual ~IB() {}
};
class A : virtual public IObject
{
public:
virtual ~A() {}
void foo() { /* stick a breakpoint here to confirm that this is called */ }
};
class T : virtual public A, virtual public IB, virtual public IC
{
public:
virtual ~T() {}
};
int main()
{
IB *b_ptr = new T;
A *a_ptr = dynamic_cast<A *>( dynamic_cast<IObject *>(b_ptr) );
a_ptr->foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我并不是说这是正确的解决方案,但它可能会提供一些关于发生了什么的信息......
有没有正确的方法来做到这一点?或者我应该实施一个解决方案?我已经考虑过IB和IC都是从A实际上继承的,但是我上次尝试过IIRC时,有一些并发症让它变得不可取.
我接着说,IB和IC的定义在你的控制之下.
COM接口在Windows上的工作方式; 这些做你想做的事,即:
这样做,你可以做类似的事情(未经测试的代码)......
interface IQueryInterface
{
IQueryInterface* queryInterface(const Guid* interfaceId);
};
interface IB : public abstract IQueryInterface
{
...
};
interface IC : public abstract IQueryInterface
{
...
};
//within your implementation class
IQueryInterface* T::queryInterface(const Guid* interfaceId)
{
if (matches(interfaceId,GUID_IB))
return (IB*)this;
if (matches(interfaceId,GUID_IC))
return (IC*)this;
if (matches(interfaceId,GUID_A))
return (A*)this;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一个更简单,更硬编码的版本是:
class A; //forward reference
interface IB
{
virtual A* castToA() { return 0; }
};
class T : public A, IB, IC
{
virtual A* castToA() { return this; }
};
Run Code Online (Sandbox Code Playgroud)
我终于明白了,丹尼尔·保罗dybnamic_cast是正确的,应该允许“横向”。我的问题是因为我的代码涉及共享库。插件 A 的类型信息在插件 B 中不可用。我的解决方案是有效地将RTLD_NOW和添加RTLD_GLOBAL到我的加载过程中
从技术上讲是
loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
Run Code Online (Sandbox Code Playgroud)
因为我使用的是 Qt 的插件系统,但有同样的区别。这些标志强制立即解析加载库中的所有符号并对其他库可见。因此,让每个需要它的人都可以使用类型信息。dynamic_cast一旦这些标志就位,就会按预期工作。
| 归档时间: |
|
| 查看次数: |
10050 次 |
| 最近记录: |