[C++-sig] How do I make correct wrappers to interfaces while using shared_ptrs?
Jim Bosch
talljimbo at gmail.com
Sun Feb 5 03:48:47 CET 2012
On 02/04/2012 08:23 PM, Adam Preble wrote:
>
>
> On Sat, Feb 4, 2012 at 10:46 AM, Jim Bosch <talljimbo at gmail.com
> <mailto:talljimbo at gmail.com>> wrote:
>
> If I understand your intend correctly, I think your class_
> definition needs to be:
>
> class_<Communicable,shared___ptr<CommunicableWrapper>,__noncopyable>
>
> (right now you have class_<CommunicableWrapper,...__>)
>
>
>
> Something like this?
>
> class_<Communicatable, boost::shared_ptr<CommunicatableWrapper>,
> boost::noncopyable>("Communicatable")
>
> I get burnt by the compiler when I try that:
>
> d:\coding\boost_1_47_0\boost\python\object\pointer_holder.hpp(217):
> error C2664: 'CommunicatableWrapper::CommunicatableWrapper(const
> CommunicatableWrapper &)' : cannot convert parameter 1 from 'PyObject *'
> to 'const CommunicatableWrapper &'
> Reason: cannot convert from 'PyObject *' to 'const
> CommunicatableWrapper'
> No constructor could take the source type, or constructor
> overload resolution was ambiguous
> It sounds like if I had a constructor that would take a PyObject *, that
> would shut it up, but I have no idea what to make of such a thing.
>
Oh, you're right. I was confusing the manual way of doing Python-side
polymorphism (derived-class holders) with the more automatic (and
better) way you're doing it (with wrapper). Your original code was fine
in that regard.
Anyhow, looking closer, here's what you need to do:
- Put your code back to what it was before I told you to change it.
- Add another line, after the class wrappers:
register_ptr_to_python< boost::shared_ptr<Communicatable> >();
That explicitly registers a shared_ptr converter for Communicatable.
I'm not sure why Boost.Python doesn't automatically do that when you
register converters for its derived classes (those are registered by
putting the shared_pr types in the template parameters to class_). It
might just be an oversight, or it might be that doing the base class
registration automatically would cause problems in some contexts.
Jim
More information about the Cplusplus-sig
mailing list