Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Boost.Python中公开指针 我有一个非常简单的C++类: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_<Tree>("Tree") .def_readwrite("head",&Tree::head) ; }_C++_Python_Boost_Boost Python - Fatal编程技术网

在Boost.Python中公开指针 我有一个非常简单的C++类: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_<Tree>("Tree") .def_readwrite("head",&Tree::head) ; }

在Boost.Python中公开指针 我有一个非常简单的C++类: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_<Tree>("Tree") .def_readwrite("head",&Tree::head) ; },c++,python,boost,boost-python,C++,Python,Boost,Boost Python,据我所知,之所以会发生这种情况,是因为Python没有指针的概念,所以它很疯狂。如何从Python中访问head变量 我知道我应该使用封装,但我目前需要一种非封装解决方案。当然,我在问了这个问题十分钟后找到了答案……下面是如何做到的: class_<Tree>("Tree") .add_property("head", make_getter(&Tree::head, return_value_policy<reference_existing_obj

据我所知,之所以会发生这种情况,是因为Python没有指针的概念,所以它很疯狂。如何从Python中访问head变量


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

当然,我在问了这个问题十分钟后找到了答案……下面是如何做到的:

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>()))
;
类(“树”) .添加_属性(“头”, 生成\u getter(&Tree::head,返回\u值\u策略()), 生成\u setter(&Tree::head,返回\u值\u策略()) ;
听起来不正确。您很可能希望
返回\u内部\u参考
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>()))
;