c++ - Boost.Python 包装层次结构避免菱形继承

标签 c++ python boost boost-python

我在寻找用 Boost.Python 包装一系列类同时避免困惑的继承问题的最佳方法时遇到了一些麻烦。假设我有具有以下结构的类 A、B 和 C:

struct A {
    virtual void foo();
    virtual void bar();
    virtual void baz();
};

struct B : public A {
    virtual void quux();
};

struct C : public A {
    virtual void foobar();
};

我想包装所有类 A、B 和 C,以便它们可以从 Python 扩展。完成此操作的常规方法是:

struct A_Wrapper : public A, boost::python::wrapper<A> {
    //dispatch logic for virtual functions
};

现在对于从 A 扩展的类 B 和 C,我希望能够继承和共享 A 的包装实现。所以我希望能够按照以下方式做一些事情:

struct B_Wrapper : public B, public A_Wrapper, public boost::python::wrapper<B> {
    //dispatch logic specific for B
};

struct C_Wrapper : public C, public A_Wrapper, public boost::python::wrapper<C> {
    //dispatch logic specific for C
}

但是,这似乎会在 B_Wrapper 和 C_Wrapper 对象中通过 boost 包装器基础的双重继承和 A 的双重继承引入各种形式的麻烦。是否有一种常见的方法可以解决我所缺少的这个实例?

谢谢。

最佳答案

一种方法是虚拟地推导:

struct B : virtual public A, ... { };
struct C : virtual public A, ... { };
struct A_Wrapper : virtual public A, ... { };

参见相关C++ FAQ Lite items注释和这意味着什么。

关于c++ - Boost.Python 包装层次结构避免菱形继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2792117/

相关文章:

c++ - 生命模拟的有效方法

python - 多级相对导入

c++ - 与 boost::python 的链接错误

c++ - Boost Signals2将插槽传递给成员函数以进行断开连接

c++ - 模板化一个简单的 Boost Proto C++ 表达式计算器

c++ - 是否存在与 Eigen::Matrix<> constexpr 构造函数相关的信息?

c++ - 改变类对象数量的 vector

python - 在 Python 中寻找 print 的替代方案

c++ - std::span 中 std::dynamic_extent 的用途是什么

python - 我如何检查一个点是否在三角形内(线上也可以)