[C++-sig] class_::add_property flexiablity No. 2
Jeff Holle
jeff.holle at verizon.net
Tue Dec 3 19:15:20 CET 2002
I'm using gcc 3.2 and boost 1.29.0 on Mandrake Linux 9.0.
I'm attempting to employ Boost.Python to wrap an accessor method that returns a reference employing the class_.add_property method.
The following C++ code compiles and links without error:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <string>
using namespace boost::python;
class Widget {
public:
Widget() : m_Name("Initial Value") {;}
const std::string& getName(void) const { return m_Name;}
private:
std::string m_Name;
};
BOOST_PYTHON_MODULE(TestAddProperty)
{
class_<Widget>("Widget")
.add_property("Name",make_function(&Widget::getName,
return_value_policy<copy_const_reference>()));
}
It does not work however. The following are two python scripts that
demonstrates this.
import TestAddProperty
widget = TestAddProperty.Widget
print widget.Name
Prints "<property object at 0x8059174>" If it worked I'd expect it to
print "Initial Value".
import TestAddProperty
widget = TestAddProperty.Widget
widget.Name = "This is a test"
print widget.Name
This prints "This is a test". If it worked, I'd expect a run-time error
executing line number 3 because I have not exposed a mutator in the
TestAddProperty module.
More information about the Cplusplus-sig
mailing list