[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
{
protected:
   Member hand_, foot_, head_;
public:
   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:

BOOST_PYTHON_MODULE(MasterModule)
{
     using namespace boost::python;
     class<Master>("Master")
       .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 
(see 
http://www.boost.org/libs/python/doc/tutorial/doc/call_policies.html
).

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


Wirawan



More information about the Cplusplus-sig mailing list