1.提供C++代码如下
class VFObj
{
public:
virtual ~VFObj() {};
virtual int func() {
return 0;
};
};
class VFObjWrapper :public VFObj, public boost::python::wrapper
{
public:
int func()
{
if (boost::python::override func = this->get_override("func"))
{
return func();
}
return VFObj::func();
}
int default_func()
{
return this->VFObj::func();
}
};
2.提供转换代码如下
BOOST_PYTHON_MODULE(Boost_Python_Sample)
{
boost::python::class_("VFObj")
.def("func", &VFObj::func, &VFObjWrapper::func);
}
3.提供python测试代码如下
obj = Boost_Python_Sample.VFObj()
print(obj.func())
class newObj(Boost_Python_Sample.VFObj):
def func(self):
return 88
obj = newObj()
print(obj.func())
纯虚函数的转换
1.提供C++代码如下
class PVFObj
{
public:
virtual ~PVFObj() {};
virtual int func() = 0;
};
class PVFObjWrapper :public PVFObj, public boost::python::wrapper
{
public:
int func()
{
return get_override("func")();
}
};
2.提供转换代码如下
BOOST_PYTHON_MODULE(Boost_Python_Sample)
{
boost::python::class_("PVFObj")
.def("func", boost::python::pure_virtual(&PVFObj::func));
}
3.提供python测试代码如下
class new2Obj(Boost_Python_Sample.PVFObj):
def func(self):
return 66
obj = new2Obj()
print(obj.func())



