c++ - 使用 Boost::Python 用参数包装纯虚拟方法

标签 c++ python interface word-wrap boost-python

我目前正在尝试使用 Boost::Python 向 Python 公开 C++ 接口(interface)(纯虚拟类)。 C++ 接口(interface)是:

代理.hpp

#include "Tab.hpp"
class Agent
{
    virtual void start(const Tab& t) = 0;
    virtual void stop() = 0;
};

并且,通过阅读“官方”教程,我成功编写并构建了下一个 Python 包装器:

代理.cpp

#include <boost/python.hpp>
#include <Tabl.hpp>
#include <Agent.hpp>
using namespace boost::python;

struct AgentWrapper: Agent, wrapper<Agent>
{
    public:
    void start(const Tab& t)
    {
        this->get_override("start")();
    }
    void stop()
    {
        this->get_override("stop")();
    }
};

BOOST_PYTHON_MODULE(PythonWrapper)
{
    class_<AgentWrapper, boost::noncopyable>("Agent")
        .def("start", pure_virtual(&Agent::start) )
        .def("stop", pure_virtual(&Agent::stop) )
    ;
}

请注意,我在构建它时没有遇到任何问题。不过,令我担心的是,正如您所看到的,AgentWrapper::start 似乎没有向 Agent::start 传递任何参数:

void start(const Tab& t)
{
    this->get_override("start")();
}

Python 包装器如何知道“start”收到一个参数?我该怎么做?

最佳答案

get_override 函数返回 override 类型的对象它对不同数量的参数有许多重载。所以你应该能够这样做:

void start(const Tab& t)
{
    this->get_override("start")(t);
}

你尝试过这个吗?

关于c++ - 使用 Boost::Python 用参数包装纯虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2277018/

相关文章:

python - 树莓派 i2c 读/写错误

python - 重新解释 Julia 中的指针

c++ - 为什么这个简单的线程 C++ 程序在退出时崩溃,除非我调用 thread.join()?

python - 在安装 django cms 之前无法激活虚拟环境

python - 远程 ipython 内核不显示图

python - 从 KML 中删除元素 (Python)

c - 这是接口(interface)还是函数原型(prototype)?

c# - 我应该通过哪个接口(interface)公开 List<T>?

c# - 混淆为什么接口(interface)在基接口(interface)已经继承的情况下又被继承了

c++ - 在 C++ 中创建文本文件时要求用户输入文本文件 (ASCII) 名称