【问题标题】:Casting a Generic Class Handle to Base Class (i.e. interface) with multiple inheritance将通用类句柄转换为具有多重继承的基类(即接口)
【发布时间】:2020-12-14 22:02:33
【问题描述】:

我正在为一个 c++ 库开发一个 c-api 包装器。我也是c++库的维护者。

到目前为止,我的基本策略是为库中的每个 c++ 类设置一个不透明的句柄。 我将句柄声明为结构:

struct Handle_;
typedef struct Handle_ *Handle;

然后在我的 c-api 包装头中,我为类的每个成员函数都有一个函数,包括创建和销毁。我已经按照这篇文章中的描述实现了它: https://stackoverflow.com/a/2045860/3237961

但知道我有以下问题:

注意:要重现问题,不需要 c-api 包装器。这都是关于铸造的。

假设我有一个继承自两个抽象类(即接口)的类:

class T_IDeviceInfo {
public:
    virtual int getDeviceType() = 0;
};

class T_IDeviceControl {
public:
    virtual void open() = 0;

};

class T_Device : public T_IDeviceInfo, public T_IDeviceControl {
public:
    int getDeviceType() override {
        std::cout << "T_Device::getDeviceType()" << std::endl;
        return 0;
    }
    void open() override {
        std::cout << "T_Device::open()" << std::endl;
    }
};

在下面的代码中,我使用了上面声明的类:

int main(){
    //Prints the device type message and the open message from T_Device
    T_Device* device = new T_Device();
    device->getDeviceType();
    device->open();

    //works
    std::cout << "cast as derived class" << std::endl;
    reinterpret_cast<T_IDeviceInfo*>(device)->getDeviceType();
    reinterpret_cast<T_IDeviceControl*>(device)->open();

    std::cout << std::endl;

    void* handle = reinterpret_cast<void*>(device);

    //works
    std::cout << "cast handle as derived class" << std::endl;
    reinterpret_cast<T_Device*>(handle)->getDeviceType();
    reinterpret_cast<T_Device*>(handle)->open();

    //does not work
    std::cout << "cast handle as base class (i.e. interface)" << std::endl;
    reinterpret_cast<T_IDeviceInfo*>(handle)->getDeviceType();
    reinterpret_cast<T_IDeviceControl*>(handle)->open();

}

输出如下所示:

T_Device::getDeviceType()
T_Device::open()
cast as derived class
T_Device::getDeviceType()
T_Device::open()

T_Device::getDeviceType()
T_Device::open()
cast handle as derived class
T_Device::getDeviceType()
T_Device::open()
cast handle as base class (i.e. interface)
T_Device::getDeviceType()
T_Device::getDeviceType()

所以问题是最后一节。当我将我的通用句柄转换为基类(接口)时,它的函数被声明了。似乎它总是调用继承顺序第一个的类的函数。 到目前为止,我所读到的是,演员现在不关心班级规模并计算错误的偏移量。它是否正确?你能帮我澄清一下吗?

我做了一些研究并注意到以下几点: 当使用static_cast&lt;&gt;() 而不是reinterpret_cast&lt;&gt;() 时,我得到了相同的输出。 我知道使用不同的铸造方法会有所不同,但我很困惑哪种方法最适合我的任务。

所有关于 c wrapper api 的帖子都提出了上述链接答案中的方法,并使用 reinterpret_cast 或 static_cast。

是否可以将通用句柄转换为派生类的基类。这会为我节省很多工作,因为接口用于不同的设备类,所以我不需要为每个设备重写 c 包装函数。

感谢任何帮助。 谢谢!

【问题讨论】:

    标签: c++ inheritance casting wrapper


    【解决方案1】:

    我认为,static_cast&lt;&gt;() 和 reinterpret_cast&lt;&gt;() 根本无法进行您要求的这种转换,因为您有多重继承。如果有多个基类,则将一个类转换为其基类会涉及一些置换魔法。

    我的建议是使用static_cast&lt;&gt;() 转换为公共类T_Device,然后使用dynamic_cast&lt;&gt;() 来完成真正的工作:

    T_Device *const device = static_cast<T_Device *>(handle);
    T_IDeviceInfo *const info = dynamic_cast<T_IDeviceInfo *>(device);
    T_IDeviceControl *const control = dynamic_cast<T_IDeviceControl *>(device);
    

    但据我了解,您有一些设备类,例如

    class T_Keyboard : public T_IDeviceInfo, publibc T_IDeviceControl { /* ... */ };
    class T_Mouse : public T_IDeviceInfo, publibc T_IDeviceControl { /* ... */ };
    

    还有一个工厂函数,例如

    void *get_device_handle(const char *name) {
        if (strcmp(name, "keyboard") == 0) {
            return new T_Keyboard(/* ... */);
        } else if (strcmp(name, "mouse") == 0) {
            return new T_Mouse(/* ... */);
        } else {
            return NULL;
        }
    }
    

    现在的问题是,你怎么能写出这样的函数

    void open_device(void *handle) {
        T_IDeviceControl *const control = /* some casting from handle */
        control->open();
    }
    

    对吗?一种可能的解决方案是,为所有接口定义一个通用的虚拟基类:

    class T_Handle {
    public:
        // By the way: You forgot this important one! In polymorphic classes
        // the destructor must be virtual.
        virtual ~T_Handle() { }
    };
    
    class T_IDeviceInfo : public virtual T_Handle { /* ... */ };
    class T_IDeviceControl : public virtual T_Handle { /* ... */ };
    

    基类T_Handle 必须是虚拟的,否则T_Mouse 和T_Keyboard 内部有两个T_Handle 基类。由于 T_IDeviceInfo 和 T_IDeviceControl 两个类都包含一个独立的基类 T_Handle 并且从 T_Mouse 向上转换到 T_Handle 将是不明确的。

    get_device_handle() 函数必须如下所示:

    void *get_device_handle(const char *name) {
        if (strcmp(name, "keyboard") == 0) {
            return dynamic_cast<T_Handle *>(new T_Keyboard(/* ... */));
        } else if (strcmp(name, "mouse") == 0) {
            return dynamic_cast<T_Handle *>(new T_Mouse(/* ... */));
        } else {
            return NULL;
        }
    }
    

    open_device() 函数如下所示:

    void open_device(void *handle) {
        T_Handle *const base = static_cast<T_Handle *>(handle);
        T_IDeviceControl *const control = dynamic_cast<T_IDeviceControl *>(base);
        control->open();
    }
    

    【讨论】:

    • 感谢您的回复!这是有效的,但对我没有多大帮助。因为我必须为每个版本的“设备”编写一个包装函数,其中句柄首先转换为“T_Device”,然后转换为它的接口。意味着在访问它的接口函数之前必须知道具体的类。因此,与为实现它的每个类包装每个接口函数相比,工作量是相同的。还是我错过了什么?
    • 再次感谢您对答案的详细扩展。我稍后会尝试实施您的建议。现在我已经做了一个解决方法,我在没有多重继承的情况下这样做。它满足了我现在的需求。但我肯定会回来检查您的解决方案。
    • 只是评论让您知道,我测试了您的建议,效果很好。非常感谢。我正在使用的所有对象都知道扩展了我将 Handle 投射到的 BaseObject(reinterpret_cast,因为我使用结构作为指针)。 BaseObject 是通过 dynamic_cast 转换为我想要的对象/接口的。我将铸件封装在自己的模板函数中。代码现在看起来更干净了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多