[C++-sig] boost python class member getter/setter same name different only by constness

Stefan Seefeld stefan at seefeld.name
Fri Oct 3 18:50:33 CEST 2014


On 2014-10-02 16:09, MM wrote:
> Hi
>
> class C {
> public:
>   const std::string& name() const;
>   std::string& name();
> private:
>   std::string name_;
> };
>
> given this class C, how would I expose it to python with the class
> property name?
>
> class_<C>("C").
>   .add_property("name",   &C::name, &C::name);
>
> or do I use 2 mem function pointers to distinguish the 2 names, and
> pass those 2 pointers?

You need to disambiguate the two overloads. The cleanest way to do that
is to introduce two (local) variables of the appropriate
pointer-to-member-function types, then pass those variables to the
'add_property' call.
In addition, I believe boost.python expects a different signature for
the setter (a function taking a value-type argument), so you may have to
provide a wrapper function for that, unless you want to modify the 'C'
class in-place.

HTH,
        Stefan



-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list