[C++-sig] wrapping reference members

William Ladwig wladwig at wdtinc.com
Wed Jul 22 17:20:12 CEST 2009


You may need to create a getter and a setter in your wrapper, and then you can use the "make_function" function to change the CallPolicy when you use create a property.  I think you want your call policy to be return_internal_reference<> and that will handle the lifetime correctly.  Here is a reference link that that technique:

http://wiki.python.org/moin/boost.python/HowTo#getterandsettermethodsasaproperty

There may be an easier solution to your problem, but I know that the technique above will work.

Hope this helps,

Bill

-----Original Message-----
From: cplusplus-sig-bounces+wladwig=wdtinc.com at python.org [mailto:cplusplus-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of Sebastian Kraemer
Sent: Wednesday, July 22, 2009 9:57 AM
To: cplusplus-sig at python.org
Subject: Re: [C++-sig] wrapping reference members

> I would think that .def_readwrite would do what you need. If it doesn't
> look at using .def_property and get/set pair. Caveat: making sure that
> 'a' stays a valid reference is your problem. Python will happily crash
> horribly (if you are lucky) in response to bogus pointers or references.
Thanks for your fast answer! I tried it with following code:

class_<Foo>("Foo", init<>())
    .def_readwrite("a", &Foo::a)
    ;

But it raises the error:

error: cannot create pointer to reference member 'Foo::a'

And it seems it's really not possible to create a pointer to a reference in c++ ;) So what else do I have to write instead of "&Foo::a"?

Cheers,
Sebastian

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list