boost: return pointer to base class

Jacek Generowicz jacek.generowicz at cern.ch
Fri Jul 26 11:43:59 EDT 2002


John Hunter <jdhunter at nitace.bsd.uchicago.edu> writes:

> >>>>> "Jacek" == Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> 
>     Jacek> 1) Expose a C++ class hierarchy to python (using Boost)
> 
>     Jacek> 2) Allow all the exposed classes to be subclassed, with
>     Jacek> correct overriding of virtual fuctions
> 
>     Jacek> 3) Expose a C++ function which returns a pointer to the
>     Jacek> base class.
> 
> In general, returning passing raw pointer in boost::python is
> problematic.

Don't I know it! Unfortunately the signatures of the functions I am
trying to expose are completery beyond by influence.

>  Have you read
> http://www.boost.org/libs/python/doc/pointers.html?

Of course :-)

> I was able to compile your code by replacing the raw pointer return
> with a shared pointer.  Will this suit your needs?
> 
> #include <boost/shared_ptr.hpp>
> 
> boost::shared_ptr<abstract> rcbpta() {
>   return boost::shared_ptr<abstract>(new concrete());
> }

I can't touch rcbpta's real-world equivalents, but I should be able to
use a wrapper:

boost::shared_ptr<abstract> rcbpta_W() {
  return boost::shared_ptr<abstract>(rcbpta());
}

I'd better add "study boost::shared_ptr" to my (rapidly lengthening)
to-do list.

Thanks very much, John ... this was really beginning to annoy me, and
your suggestion looks very promising at the moment.



More information about the Python-list mailing list