我有一个抽象类A2,派生类B2。我使用boost python将C ++映射到python
我想映射一个函数,返回一个A2的boost :: shared_ptr
这个抽象类被包装
struct A2{
virtual ~A2(){}
virtual int ret() =0;
};
struct B2: public A2{
virtual ~B2(){}
int ret() { return 1; }
};
// wrapper
struct A2Wrap : A2, wrapper<A2>
{
inline int ret()
{
return this->get_override("ret")();
}
};
// function to map
boost::shared_ptr<A2> f1()
{
return boost::shared_ptr<A2>(new B2());
}
// expose to python
BOOST_PYTHON_MODULE(grids)
{
class_<A2Wrap, boost::shared_ptr<A2Wrap> , boost::noncopyable >("A2");
class_<B2,boost::shared_ptr<B2>,bases<A2>>("B2");
def("f1",f1);
}
执行时得到以下结果:
>>>import grids
>>> a = grids.f1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr<A2>
对这个问题有任何想法吗?