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

代理程式

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

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

Agent.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包装器将如何知道“开始”收到一个参数?我该怎么办?

最佳答案

get_override函数返回一个类型为override的对象,该对象对于不同数量的参数具有多个重载。因此,您应该能够做到这一点:

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

你有尝试过吗?

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

10-11 22:37
查看更多