class B {};
class A {
public:
// original C++ class interface
A(std::shared_ptr<B> pb) : mb(pb){}
// have to add this for pybind11, since pybind11 doesn't take shared_ptr as argument.
A(B * pb):A(std::shared_ptr<B>(pb)){}
private:
std::shared_ptr<B> mb;
}
namespace py = pybind11;
PYBIND11_MODULE(test, m)
{
py::class_<B>(m, "B")
.def(py::init<>());
py::class_<A>(m, "A")
.def(py::init<B *>());
}
然后在python中,我将按如下方式使用它们:
^{pr2}$这是很好的,只要我不删除.a,当我在Python中删除A时,在C++的“A”中创建的SyrdYPPTMB将尝试销毁由Python和Burn管理的B对象。所以,我的问题是,C++中是否有一些智能指针不能获取原始指针的所有权?弱的\u ptr不起作用,因为我仍然需要创建一个共享的\u ptr。
https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html#std-shared-ptr
相关问题 更多 >
编程相关推荐