11

我有这个非常简单的 C++ 类:

class Tree {
    public:
        Node *head;
};
BOOST_PYTHON_MODULE(myModule)
{

   class_<Tree>("Tree")
        .def_readwrite("head",&Tree::head)
    ;

}

我想从 Python 访问 head 变量,但我看到的消息是:

No to_python (by-value) converter found for C++ type: Node*

据我了解,发生这种情况是因为 Python 吓坏了,因为它没有指针的概念。如何从 Python 访问 head 变量?

我知道我应该使用封装,但我目前坚持需要非封装解决方案。

4

1 回答 1

20

当然,我在问这个问题十分钟后找到了答案......这是它的完成方式:

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>()))
;
于 2010-03-29T21:55:41.983 回答