揭露Boost.Python的指针 [英] Exposing a pointer in Boost.Python
本文介绍了揭露Boost.Python的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个非常简单的C ++类:
I have this very simple C++ class:
class Tree {
public:
Node *head;
};
BOOST_PYTHON_MODULE(myModule)
{
class_<Tree>("Tree")
.def_readwrite("head",&Tree::head)
;
}
我要访问在Python头变量,但我看到的信息是:
I want to access the head variable from Python, but the message I see is:
No to_python (by-value) converter found for C++ type: Node*
据我了解,这是因为Python的是吓坏了,因为它没有指针的概念。如何从Python中访问头变量?
From what I understand, this happens because Python is freaking out because it has no concept of pointers. How can I access the head variable from Python?
我明白我应该使用的封装,但我目前用卡需要一个非封装解决方案。
I understand I should use encapsulation, but I'm currently stuck with needing a non-encapsulation solution.
推荐答案
当然,我找到了答案10分钟问这个问题后......这里是它是如何做的:
Of course, I find the answer ten minutes after asking the question...here's how it's done:
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>()))
;
这篇关于揭露Boost.Python的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文