[C++-sig] boost python references

Roman Yakovenko roman.yakovenko at gmail.com
Sun Apr 16 12:53:36 CEST 2006


On 4/15/06, david stummer <stummer at gmail.com> wrote:
> hi guys,
>
> i am trying to use references with boost python.
>
> in python you can do:
>
> class person:
>     name='david'
>
> a=person()
> b=a
> print a.name
> a.name='john'
>  print b.name
>
> because b is a reference, it will always change as a changes. hence b.name
> is also john.
>
> i want this functionality between boost and python.
>
> I have the C++ class:
>
> class_<Window>("Window")
>     .def_readwrite("seat", &Window::seat)
>  ;
>
> where seat is an object of class Seat. Seat contains an integer attribute
> named position. It's default value is 45.
>
> How can i used references?

There is one important difference between Python code and C++.
In Python you create value that belongs to class, while in C++ you
create value that
belongs to class instance. I think you should use add_static_property function.
Also, I think you will need to use boost::python::make_function in order to
construct getter/setter with desired call policies.

http://www.boost.org/libs/python/doc/v2/class.html
http://www.boost.org/libs/python/doc/v2/make_function.html#make_function-spec

> thanks

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list