[C++-sig] Passing ownership to C++ through pointer

Jim Bosch talljimbo at gmail.com
Tue Nov 8 17:02:23 CET 2011


On 11/08/2011 01:12 AM, Jay Riley wrote:
> I'm trying to pass an object from python to C++. I want C++ to take
> control of the object.

<snip>

> Am I doing something wrong? base on that snippet it should work. Is
> there another way to do this? I know I should probably switch to
> smart_ptrs and I'm considering it, but Id like to be able to know how to
> do this, even if its just for future reference. Any help would be
> appreciated.
>

I suspect the inheritance is getting in the way, and Boost.Python isn't 
being as smart as it could be about converting auto_ptr<T> to 
auto_ptr<U> when T* is convertible to U*.

You might want to try having your register function wrapper accept 
auto_ptr<UseComponentWrap> (and if necessary, providing an overload that 
takes auto_ptr<UseComponent>).

Before that, however, you might want to just try using "auto_ptr &" 
rather than simple "auto_ptr".  It'd be unfortunate if that was 
necessary, but it's a possibility.

Good luck!

Jim


More information about the Cplusplus-sig mailing list