![](/img/trans.png)
[英]Boost.Python: inheret from C++ object in python: Cannot add to C++ base class list
[英]boost.python: Calling function of a base class from Python
我有一个暴露了C ++类的模块:
class MyClass{
public:
MyClass(){}
void foo(){
//...
}
};
BOOST_PYTHON_MODULE(my_module){
class_<MyClass>("MyClass", init<>())
.def("foo", &MyClass::foo)
;
}
在Python中,我创建了一个派生自此类的类。
from my_module import *
class Derived(MyClass):
def __init__(self):
self.foo()
d = Derived()
我收到错误:
Boost.Python.ArgumentError: Python argument types in
MyClass.foo(Derived)
did not match C++ signature:
foo(class MyClass {lvalue})
我希望能够从派生类中调用基类中定义的函数。 我怎样才能解决这个问题?
看起来你需要在调用foo
之前调用MyClass.__init__(self)
; 否则foo
没有获得MyClass
实例。 使用super()的加分点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.