Vla*_*ala 11 c++ python boost boost-python
我有这个非常简单的C++类:
class Tree {
public:
Node *head;
};
BOOST_PYTHON_MODULE(myModule)
{
class_<Tree>("Tree")
.def_readwrite("head",&Tree::head)
;
}
Run Code Online (Sandbox Code Playgroud)
我想从Python访问head变量,但我看到的消息是:
No to_python (by-value) converter found for C++ type: Node*
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这是因为Python没有指针的概念,所以这种情况很糟糕.如何从Python访问head变量?
我知道我应该使用封装,但我目前仍然需要一个非封装解决方案.
Vla*_*ala 19
当然,在提出问题后十分钟我找到答案......这是如何完成的:
class_<Tree>("Tree")
.add_property("head",
make_getter(&Tree::head, return_value_policy<reference_existing_object>()),
make_setter(&Tree::head, return_value_policy<reference_existing_object>()))
;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2446 次 |
| 最近记录: |