Boost Python: polymorphic container?

久未见 提交于 2019-12-03 16:22:12

First, make sure your classes are indeed polymorphic (i.e. they have at least one virtual function or a virtual destructor). Your example above doesn't, though I'm sure your real use case does. Without that none of Boost.Python's RTTI-based machinery for polymorphism will work.

Then, if you've exposed both classes with Boost.Python and registered shared_ptr converters for them:

#include <boost/python.hpp>

namespace bp = boost::python;

BOOST_PYTHON_MODULE(example) {
    bp::class_<A >("A");
    bp::register_ptr_to_python< boost::shared_ptr<A> >();
    bp::class_< B, bp::bases<A> >("B");
    bp::register_ptr_to_python< boost::shared_ptr<B> >();
}

...that's all you need to do to make sure Python only ever sees the most-derived type. There's no need to do anything special to ensure A is casted to B when possible.

That still leaves the question of how to wrap a function that returns a container. The simplest is probably to use the indexing suite included with Boost.Python:

http://www.boost.org/doc/libs/1_49_0/libs/python/doc/v2/indexing.html

There are other options floating around the web (including a "version 2" of the indexing suite that is better in many respects, but isn't included with Boost.Python), but for simple problems this is probably the most convenient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!