【发布时间】:2012-05-18 17:13:37
【问题描述】:
考虑以下示例:
#include "Python.h"
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
class A {};
class B : public A{};
void foo(boost::shared_ptr<A>& aptr) { }
BOOST_PYTHON_MODULE(mypy)
{
using namespace boost::python;
class_<A, boost::shared_ptr<A> >("A", init<>());
class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>());
def("foo", foo);
}
如果我调用 python 代码
import mypy
b = mypy.B()
mypy.foo(b)
我明白了
ArgumentError: Python argument types in
mypy.foo(B)
did not match C++ signature:
foo(boost::shared_ptr<A> {lvalue})
我已经用谷歌搜索了很多,但我找不到很好的解释/修复/解决方法。非常欢迎任何帮助!
【问题讨论】:
-
如果
foo采用boost::python::object参数,然后从中提取shared_ptr会发生什么? -
当然可以,但我希望有一种方法可以不用为 foo 编写任何额外的包装器(我需要很多包装器)
标签: c++ python boost shared-ptr boost-python