【问题标题】:Wrapping a pure virtual method with arguments using Boost::Python使用 Boost::Python 用参数包装纯虚方法
【发布时间】:2011-01-17 15:13:20
【问题描述】:

我目前正在尝试使用 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 in:

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

python 包装器如何知道“开始”接收一个参数?我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    get_override 函数返回一个override 类型的对象,该对象具有不同数量的参数的多个重载。所以你应该可以这样做:

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

    你试过了吗?

    【讨论】:

    • 我已经这样做了。它似乎有效。非常感谢。
    猜你喜欢
    • 2019-08-18
    • 2011-10-03
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2014-03-03
    • 2023-03-25
    相关资源
    最近更新 更多