![](/img/trans.png)
[英]override a C++ virtual function within Python with Boost.python?
[英]Extending a virtual C++ class exposed via Boost.Python
我正在尝试使用Boost.Python公开此C ++类:
class VAlgorithm {
public:
VAlgorithm(const char *name);
virtual ~VAlgorithm();
virtual bool Initialize() = 0;
virtual bool Process() = 0;
virtual bool Finalize() = 0;
virtual const char *GetName() const; // Default implementation in cpp file
}
我的最终目标是在python shell中将VAlgorithm的子级定义为python类。 在此示例之后 ,我定义了一个回调类:
class VAlgorithm_callback: public VAlgorithm {
public:
VAlgorithm_callback(PyObject *p, const char *name) :
self(p), VAlgorithm(name) {
}
const char * GetName() const {
return call_method<const char *>(self, "GetName");
}
static const char * GetName_default(const VAlgorithm& self_) {
return self_.VAlgorithm::GetName();
}
private:
PyObject *self;
};
现在,我只公开类本身和GetName()方法。 由于它是一个虚拟类,因此将这段代码放在BOOST_PYTHON_MODULE内:
class_<VAlgorithm, VAlgorithm_callback, boost::noncopyable>("VAlgorithm", no_init) //
.def("GetName", &VAlgorithm_callback::GetName_default); //
我可以编译它并将模块加载到python shell中。 然后,我尝试定义一个子类,并调用C ++代码中定义的GetName()默认实现:
>>> class ConcAlg1(VAlgorithm):
... pass
...
>>> c1 = ConcAlg1("c1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python
>>> class ConcAlg2(VAlgorithm):
... def __init__(self, name):
... pass
...
>>> c2 = ConcAlg2("c2")
>>> c2.GetName()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
VAlgorithm.GetName(ConcAlg2)
did not match C++ signature:
GetName(VAlgorithm)
>>> class ConcAlg3(VAlgorithm):
... def __init__(self, name):
... super(ConcAlg3, self).__init__(self, name)
...
>>> c3 = ConcAlg3("c3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
RuntimeError: This class cannot be instantiated from Python
我不是专家(只是第一次面对这些问题),但是在我看来,ConcAlg1和ConcAlg3试图实例化一个VAlgorithm对象并由于暴露VAlgorithm时使用了no_init参数而失败(我无法忽略它,或者代码将无法编译),并且ConcAlg2无法调用GetName(),因为某种程度上它不能被识别为VAlgorithm的子代。 我一定在做一些琐碎的错误,但是我不知道该怎么办(我是Boost.Python和扩展的新手)。 谁能帮帮我吗? 谢谢
我想我找到了解决方案。 通过我在提问中引用的示例以及此处的boost文档,我发现问题在于回调类是Boost.Python包装的真实类,它必须是一个具体的类。 因此,我在其中实现了缺少的纯虚拟方法:
class VAlgorithm_callback: public VAlgorithm {
public:
VAlgorithm_callback(PyObject *p, const char *name) :
self(p), VAlgorithm(name) {
}
virtual bool Initialize() {
return call_method<bool>(self, "Initialize");
}
virtual bool Process() {
return call_method<bool>(self, "Process");
}
virtual bool Finalize() {
return call_method<bool>(self, "Finalize");
}
const char * GetName() const {
return call_method<const char *>(self, "GetName");
}
// Supplies the default implementation of GetName
static const char * GetName_default(const VAlgorithm& self_) {
return self_.VAlgorithm::GetName();
}
private:
PyObject *self;
};
并将包装器修改为:
class_<VAlgorithm, boost::shared_ptr<VAlgorithm_callback>, boost::noncopyable ("VAlgorithm", init<const char *>()) //
.def("Initialize", &VAlgorithm_callback::Initialize)
.def("Process", &VAlgorithm_callback::Process)
.def("Finalize", &VAlgorithm_callback::Finalize)
.def("GetName", &VAlgorithm_callback::GetName_default);
现在,我可以在Python中定义一个子类并调用默认的GetName()方法:
>>> class ConcAlg(VAlgorithm):
... pass
...
>>> c = ConcAlg("c")
>>> c.GetName()
'c'
>>>
我所做的事情与此非常相似。 您为什么不遵循评论中已经找到的内容?
当您在Python中创建派生于VAlgorithm的类的实例时,必须用C ++实例化VAlgorithm_callback来表示它。 如果没有在BOOST_PYTHON_MODULE中声明任何构造函数,则是不可能的。
按照示例中的方式进行操作应该可以:
class_<VAlgorithm, VAlgorithm_callback, boost::noncopyable>("VAlgorithm", init<std::string>())
定义子类的init时,可以使用以下语法调用父构造函数(但是您的代码也可以工作,这就是我的方法)。 在您使用的示例中,它们甚至都没有定义init,因此调用了父级init 。
class ConcAlg3(VAlgorithm):
def __init__(self, name):
VAlgorithm.__init__(self, name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.