boost::python and shared_ptr

Alex Martelli aleax at aleax.it
Tue Aug 21 04:27:31 EDT 2001


"John Hunter" <jdhunter at nitace.bsd.uchicago.edu> wrote in message
news:1ritfiny0o.fsf at video.bsd.uchicago.edu...
    ...
> SomeClass's C++ constructor looks like this:
>   typedef boost::shared_ptr<Base> PtrBase;
>   SomeClass( PtrBase )
>
> So here is my problem: if I create an instance if Derived in python,
> how do I pass it to SomeClass
>
> d = Derived()
> s = SomeClass( d )  //nope: d is not a shared_ptr<Base>
>
> I would expect this problem would arise frequently: Any suggestions
> as to how to solve it?

What about adding (Python-side only maybe) another 'constructor'
(factory, if you will) of SomeClass that's a template?  If your
C++ accepts templated constructors,

class SomeClass {
    ...
    template <class Derived>
    SomeClass(boost::shared_ptr<Derived>) {
        // ... whatever ...
    }

If your C++ compiler isn't up to this it should still be
possible to simulate such a constructor with a factory
function declared to Boost Python as a 'constructor'.  I
haven't tested this, but it seems it should work...


Alex







More information about the Python-list mailing list