通过Python BOOST使用派生类(c++)
Using derived class (C++) by Python BOOST
我有一个c++类,我想在Python中使用:
#include "ExtraClass.h"
class CApp
{
...
ExtraClass Bar; //there is function Foo()
}
BOOST_PYTHON_MODULE( CApp )
{
class_<ExtraClass>("ExtraClass",init<>())
.def("Foo",&ExtraClass::Foo)
;
class_<CApp>("CApp", init<>())
.def_readonly("Bar", &CApp::Bar)
;
可以编译。这是CApp。在Python中导入的so文件。所以问题从Python开始:
from CApp import *
class pyApp(CApp):
def __init__(self):
print "<--INIT-->"
CApp = CApp()
pyApp = pyApp()
print CApp.Bar.Foo()
print pyApp.Bar.Foo()
输出:<--INIT-->
FOO // <- this is from CApp.Bar.Foo()
Traceback (most recent call last):
File "./pytest.py", line 16, in <module>
print pyApp.Bar.Foo()
Boost.Python.ArgumentError:
Python argument types in None.None(pyApp)
did not match C++ signature: None(CApp {lvalue})
如果在派生类中实现init(),则需要确保调用基类init(),否则它将没有基类成员。
from CApp import *
class pyApp(CApp):
def __init__(self):
CApp.__init__(self)
print "<--INIT-->"
CApp = CApp()
pyApp = pyApp()
print CApp.Bar.Foo()
print pyApp.Bar.Foo()
我遇到了同样的问题,并找到了解决方案,感谢这里- https://stackoverflow.com/a/6396839.
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 在 C++ 中用派生类型重写成员函数
- 具有多个类、派生类的C++正向声明
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 如果基类包含双指针成员,则派生类的构造函数
- 为什么此派生对象无法访问基类的后递减方法?
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践