0

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?

For example, how would I wrap x from the following class:

class Foo{
    int * x;
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
user810973
  • 161
  • 8

1 Answers1

-1

First you need to expose the class and properties to 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.

>>> import mylib
>>> fooObj = mylib.Foo()
>>> fooObj.x = 3
>>> print 'fooObj.x is ', fooObj.x
fooObj.x is 3
blockchaindev
  • 3,134
  • 3
  • 22
  • 30
  • When I tried this, I've got:ArgumentError: Python argument types in None.None(Foo, int) did not match C++ signature: None(Foo {lvalue}, int*) – enobayram Apr 06 '12 at 22:37
  • Also, if you try to read the variable, you get: `Traceback (most recent call last): File "", line 1, in TypeError: No to_python (by-value) converter found for C++ type: int*` – user810973 Apr 10 '12 at 12:32