Boost.Python: Ownership of pointer variables

我只是一个虾纸丫 提交于 2019-12-06 03:16:00

Answering my own question:

I've missed an FAQ entry in the Boost.Python documentation that gave me the right hint:

//The node class should be held by std::auto_ptr
class_<Node, std::auto_ptr<Node> >("Node")

Create a thin wrapper function for the add_child method:

void node_add_child(Node& n, std::auto_ptr<Node> child) {
   n.add_child(child.get());
   child.release();
}

Complete code to expose the node class:

//The node class should be held by std::auto_ptr
class_<Node, std::auto_ptr<Node> >("Node")
//expose the thin wrapper function as node.add_child()
.def("addChild", &node_add_child)
;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!