简体   繁体   English

在boost.python/pyplusplus中包装int指针成员变量

[英]Wrap int pointer member variable in boost.python/pyplusplus

If I am using boost.python or pyplusplus, how do I wrap an int pointer, or any pointer that is a member variable of a class?如果我使用的是 boost.python 或 pyplusplus,我该如何包装一个 int 指针,或者任何作为 class 成员变量的指针?

For example, how would I wrap x from the following class:例如,我将如何包装来自以下 class 的x

class Foo{
    int * x;
}

First you need to expose the class and properties to Python.首先,您需要将 class 和属性公开给 Python。

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(mylib)
{
    using namespace boost::python;
    class_<Foo>("Foo")
        .def_readwrite("x", &Foo::x);
}

Invoking the class in Python is similarly simple.在 Python 中调用 class 同样简单。

>>> import mylib
>>> fooObj = mylib.Foo()
>>> fooObj.x = 3
>>> print 'fooObj.x is ', fooObj.x
fooObj.x is 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM