[C++-sig] class properties for functions returning references

Renato Araujo renatox at gmail.com
Thu Nov 27 21:33:11 CET 2008


Hi anirudh,

Try something like that:

...
   .add_property("bar", make_function(&Foo::get_bar,
return_value_policy<reference_existing_object>()) );
....

I think "make_getter" is used to create access to members in the class.

BR
Renato


2008/11/27 anirudh vij <anirudhvij at gmail.com>:
> Hi,
>
> I've been trying to make the following code work
>
> class Bar{
> public:
>     Bar():_int(4){};
>     ~Bar(){};
>     int get()
>     {return _int;}
> private:
>     int _int;
> };
>
> class Foo
> {
> public:
>     Foo():_bar(Bar()){};
>     ~Foo(){};
>     Bar& get_bar(){return _bar;}
> private:
>     Bar _bar;
> };
>
> #include <boost/python.hpp>
>
> BOOST_PYTHON_MODULE(test)
> {
>     using namespace boost::python;
>     class_<Bar>("Bar")
>     .def("get",&Bar::get)
> ;
>     class_<Foo>("Foo")
>       .add_property("bar",
> make_getter(&Foo::get_bar,return_value_policy<copy_non_const_reference>())
> )
>     ;
> }
>
> The idea is to expose the _bar member via the get() function. I dont want to
> use def_readonly (since this is a dummy test case for a more general problem
> I am facing of using getters with functons returning references)
>
> I get a compile error, with the source of the error in the return value
> policy . The error log is attached.
>
> I also tried return_internal_reference<>(), and
> return_value_policy<return_by_value>, but both give compilation errors.
>
> Any help is appreciated
>
> regards,
> anirudh
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Renato Araujo Oliveira Filho


More information about the Cplusplus-sig mailing list