[C++-sig] How to wrap reference-returning method as a property?

Wirawan Purwanto wirawan0 at softhome.net
Tue Jul 6 19:37:41 CEST 2004

This is a newbie question again. Suppose I have a C++ program like this:

struct Member
   int x; // whatever...

struct Master
   Member hand_, foot_, head_;
   Member &hand() { return hand_; }

Master foo;

You see that the reference returned by hand() has a lifetime defined by 
the Master object. Now I want to wrap this to python:

     using namespace boost::python;
       .add_property("hand", &Master::hand); // wouldn't work

That interface wouldn't work since the reference's lifetime wasn't 
known/specified. Is there anyway to fix this problem? I did not see the 
"recipe" to do it in the tutorial. It only deals with regular function 

Alternatively I could use .def instead of .add_property. But I'd like to 
use .add_property if I can.


More information about the Cplusplus-sig mailing list