[C++-sig] Re: Shared Pointers and Boost.Python

David Abrahams dave at boost-consulting.com
Sat Jul 24 16:39:50 CEST 2004


"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> writes:

> --- Brian Hall <bhall at gamers-fix.com> wrote:
>> BOOST_PYTHON_MODULE(GI_AISDK)
>> {
>> 	// Expose the State class to Python
>> 	class_<A>("A");
>> 	class_<B>("B", init<shared_ptr<A> >())
>> 		.def("getA", &B::getA);
>> }
>
> Try:
>         class_<A, shared_ptr<A> >("A");
>         class_<B, shared_ptr<B> >("B", init<shared_ptr<A> >())
>
> This should answer the first part of your question. I am not sure about the
> second part.

Actually, everything he did was fine.  Wrapping classes with
shared_ptr is almost completely pointless now, since any Python object
holding an X object can be converted to a shared_ptr<X>.  The
shared_ptr has a custom deleter that manages the owning Python object's
lifetime.

You might want to use register_ptr_to_python<shared_ptr<X> >() for
some shared pointers if you ever create them from C++ code, but in
this case it never happens so they can all be automatically converted
back into Python objects by (we just extract the owning Python object
from the deleter).

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list