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

MM finjulhich at gmail.com
Fri Oct 3 18:56:12 CEST 2014


yes i did that.

class C {
> public:
>   const std::string& get_name() const;
>   void set_name(const std::string&);
> private:
>   std::string name_;
> };
>


> class_<C>("C").
>   .add_property("name",   &C::get_name, &C::set_name);


this fails to compile because of unspecified call policies about the string
refs.


The following, on the other hand, compiles.

> class C {
> public:
>   const std::string get_name() const;
>   void set_name(const std::string);
> ....
> class_<C>("C").
>   .add_property("name",   &C::get_name, &C::set_name);


Which policy do I specify? and how do I set it in add_property?

MM

On 3 October 2014 17:50, Stefan Seefeld <stefan at seefeld.name> wrote:

> 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...
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20141003/34c548c0/attachment.html>


More information about the Cplusplus-sig mailing list