2

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

代理.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 包装器如何知道“开始”接收一个参数?我该怎么做?

4

1 回答 1

4

get_override 函数返回一个覆盖类型的对象,该对象具有不同数量的参数的多个重载。所以你应该能够做到这一点:

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

你试过这个吗?

于 2010-02-17T01:28:10.627 回答