BOOST.PYTHON中的基础和派生类
Base and derived classes in Boost.Python
我有两个类 - 假设A和B。B类是A的子类,都具有函数H,因此:
:class A{
public:
A(){;;} // ctor
virtual ~A(){ ;; } // destr.
virtual double H(int i,int j){
std::cout<<"Calling the base function H. Level is too abstractn";
exit(0);
return 0.0;
}// H
};// class A
派生的B类是:
class B : public A{
public:
B(){;;} // ctor
~B(){ ;; } // destr.
double H(int i,int j){
return 1.0;
}// H
};// class B
我还具有将类型类型的参数的函数:
double f(A x){
return x.H(1,1);
}
我想将派生类型B的对象用作Python中此函数的参数。因此,我导出了这两个类别和此处解释的功能:
BOOST_PYTHON_MODULE(modAB){
class_<A>("A", no_init);
class_<B, bases<A> >("B", init<>())
.def("H", &B::H)
;
def("f", &f);
}
所以在python中我做:
>>> from modAB import *
>>> x = B()
>>> res = f(x)
作为输出,我看到该方法H是从基类调用的,而不是从我预期的派生类中调用:
Calling the base function H. Level is too abstract
所以,我的问题是我在哪里错了,我可能想念什么?
double f(A x)
通过值传递 x
,因此传递类型B
的对象将切除其派生的虚拟函数。
要修复它,您需要通过const A&
:
double f(const A& x)
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- 在派生函数中指定void*参数
- 从从该抽象类派生的 Python 对象强制转换C++抽象类C++
- 从Boost Python包装工厂创建一个派生的类
- BOOST.PYTHON中的基础和派生类
- 助推.Python未能解析传递给函数的派生类型
- 在boost python中提取c++派生的类对象
- 如何在boost python中公开派生类函数
- 使用纯虚函数的 boost python 中的多个派生类
- 使用BoostPython从c++类创建派生的python类
- Boost Python运行时错误,将派生类型的对象从Python传递给c++函数,期望将shared_ptr传递给基类
- 派生自SWIGged Python中的c++基类
- Boost Python:无法调用C++虚函数,该函数在 Python 派生类中被覆盖
- 通过Python BOOST使用派生类(c++)