【问题标题】:Exposing a pointer in Boost.Python在 Boost.Python 中暴露一个指针
【发布时间】:2011-02-02 05:49:42
【问题描述】:

我有一个非常简单的 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 变量?

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

【问题讨论】:

    标签: c++ python boost boost-python


    【解决方案1】:

    当然,我在提问十分钟后就找到了答案……这是怎么做的:

    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>()))
    ;
    

    【讨论】:

    • 这听起来不正确。很可能你想要return_internal_reference
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多