[C++-sig] .def versus .add_property

gregsteele at speakeasy.net gregsteele at speakeasy.net
Wed Jan 19 07:36:38 CET 2005


I want to add a accessor function to a python class, but give it member semantics. When I use .def to add the accessor, everything works fine. When I use .add_property, I get compiler errors w/ Visual C++ 7.1. I have included example code below.

struct vec_t {
	int len;
	int * x;
	vec_t(int length) 
	{
		len = length;
		x = (int *) malloc(length*sizeof(int));
	}
	~vec_t() {
		if (x) free(x);
	}

	int get_len() {return len;}
};

struct outer {
	vec_t v;

	outer( int len ) : v(len) {}
	~outer() {}
	vec_t & get_v() {
		return (v);
	}
};


BOOST_PYTHON_MODULE(test_add_property)
{
	class_<vec_t, boost::noncopyable>("vec", no_init)
		.add_property("len", &vec_t::get_len );
	;

	class_<outer, boost::noncopyable >("outer", init<int>() )
              // this works fine
	      //.def("v", &outer::get_v, return_internal_reference<>() ) 

              // this doesn't compile	
             .add_property("v", &outer::get_v, return_internal_reference<>() ) 
	;
}

Thanks.

Greg





More information about the Cplusplus-sig mailing list