[C++-sig] Containers for derived classes

abeyer at uni-osnabrueck.de abeyer at uni-osnabrueck.de
Wed Aug 6 15:04:24 CEST 2003

I would like to do the following:

In C++:
Class "C" is a container holding objects of type "A".

In Python:
Class "B" is derived from class "A" and instances of "B" get stored in
containers of type "C".

When I store objects of Type B in C, they get 'downcasted' to A. Objects
returned from the container are hence no longer of type B, but of type A.
Of course I could store the original boost::python::object somewhere.
However, the FAQ says there are more elegant ways by using
"When a shared_ptr<X> is converted back to Python, the library checks to
see if it's one of those "Python object managers" and if so just returns
the original Python object."

I have played with it, but I had no luck so far.
Has anyone an example using shared_ptr that fits my purpose?


More information about the Cplusplus-sig mailing list