[C++-sig] [Python] Wrapping Visitor Pattern and Object Slicing

Dave Abrahams dave at boostpro.com
Tue Dec 27 23:01:47 CET 2011

The following message is a courtesy copy of an article
that has been posted to gmane.comp.lib.boost.user as well.

on Tue Dec 27 2011, Alex Olivas <olivas-AT-icecube.umd.edu> wrote:

> Hey All,
> I'm trying to wrap a third party lib that uses the visitor
> pattern and their own smart pointers ('ref_ptr' below), which
> manages objects that inherit from 'Referenced'.  I've put
> together a simple example that illustrates the problem.
> Code for python bindings : http://codepad.org/wq47m5CI
> Simple test script : http://codepad.org/Jfme3U6U
> Output for two cases : http://codepad.org/gbFBxy04
>    Case 1) 'apply' override is commented out in the python class
>    Case 2) 'apply' override exists
> The problem that I'm having is that the 'apply' method
> ( lines 83 - 86 ) slices everything to base unless I use
> boost::ref().  However this only works for wrapped classes
> and in this lib several classes use derived visitors that
> are not part of the public API ( which I modeled as HelperVisitor
> in the above example ).
> Is there anyway to prevent object slicing for both wrapped
> and unwrapped objects in this scenario?
> I'm building against boost 1.47 and if anyone's interested in
> digging deeper the 3rd party lib is Open Scene Graph 3.0.1.

Just a wild guess, but it looks like you're copying and slicing the
VisitorBase object here:

  VisitorBase_wrapper( PyObject* o , const VisitorBase& b) : self( o ), VisitorBase(b) {};

Also, you could try using & (address-of) instead of boost::ref.

Also, I suggest following this post up on the C++-sig instead of here;
you'll get better help.


Dave Abrahams
BoostPro Computing

More information about the Cplusplus-sig mailing list