c++ - 不同编译器的C++虚继承实现不兼容?

标签 c++ visual-c++ c++builder virtual-inheritance

我有这样的公共(public)接口(interface)层次结构:

struct ISwitchable {
    /* Obtain pointer to another implemented interface of the same instance. */
    virtual int switch(unsigned int interfaceId, void** pInstance) = 0;
};
struct IFoo : public ISwitchable { /* Methods */ };
struct IBar : public ISwitchable { /* Methods */ };
struct IFooBar : public IFoo, public IBar { /* Methods */ };

实现 IFooBar 的类与工厂函数一起放入 dll 中。客户端代码加载 dll,使用工厂函数创建类实例并根据接口(interface)使用它(它们作为头文件提供)。

该方案适用于 MSVC 制作的 dll 和 Borland C++ Builder 6 制作的客户端代码。

我在层次结构中引入虚拟继承:

struct IFoo : public virtual ISwitchable { /* Methods */ };
struct IBar : public virtual ISwitchable { /* Methods */ };

当在相同的情况下(MSVC 的 dll,Builder 的客户端)客户端代码请求类的实例时,他会使用困惑的 vtable 获取它。

除了回滚到普通继承还有什么解决办法吗?

最佳答案

我不认为您可以指望任何构建的类在编译器之间兼容。 Borland 是否声称他们可以加载 MSVC 构建的类并与之互操作。如果是这样,看起来他们有一个错误。据我所知,C++ 规范中没有关于 VTable 的确切结构的任何内容,因此预计它不会跨编译器工作。

关于c++ - 不同编译器的C++虚继承实现不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1038667/

相关文章:

c# - Xamarin 和 C++ 代码

c++ - 具有参数评估错误的重载函数

c++ - 将 Visual C++ 代码转换为 Borland C++Builder

c++ - 为什么我不能在即时窗口中索引 std::vector?

c++ - new/malloc 似乎如何记住分配的数据量?

debugging - 异常时无法正确获取调用堆栈

c++ - 如何快速上手使用VC++、C++、DirectX进行游戏编程?

通过归档的 C++ 优先级

delphi - 如何使 Delphi 在 hpp 文件中发出函数和过程定义?

c++ - 将 unicode 转换为 char