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