[C++-sig] Python-overridable method taking const ref?

David Abrahams dave at boostpro.com
Thu Feb 5 03:56:12 CET 2009


on Wed Feb 04 2009, Paul Melis <pyplusplus-AT-assumetheposition.nl> wrote:

> Hello,
>
> Classes with protected destructors (as found when custom reference
> counting is used) seem to cause some trouble when using bp::wrapper<>
> containing  a method that takes a const reference to a refcounted class.
>

> struct CallbackWrap : Callback, bp::wrapper<Callback>
> {
>     void execute(const Thing& t)
>     {
>         if (bp::override ovr = this->get_override("execute"))
>         {
>             fprintf(stderr, "have override, calling...\n");
>             ovr(t);
              ^^^^^^
The problem is here, right?

If you want to pass an object by reference to a python function, you
have to wrap it in boost::ref().  Otherwise, it will try to copy the
object and the resulting copy will have to be destroyed after the call.

>             fprintf(stderr, "done calling\n");
>         }
>         else
>             Callback::execute(t);
>     }
>
>     void default_execute(const Thing& t)
>     {
>         fprintf(stderr, "default\n");
>         this->Callback::execute(t);
>     }
> };

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com


More information about the Cplusplus-sig mailing list