Boost Python Runtime error when passing object of derived type from python to C++ function expecting a shared_ptr to base type

混江龙づ霸主 提交于 2019-11-29 16:36:31

Boost.Python needs to be aware that a smart pointer to Derived can be converted to a smart pointer to AbstractBase. This can be accomplished by either of the following:

  • Using boost::shared_ptr. Boost.Python has code to handle implicit conversions between boost::shared_ptrs when their element_type are hierarchical.
  • Register an implicit conversion from std::shared_ptr<Derived> to std::shared_ptr<AbstractBase> via boost::python::implicitly_convertible. std::shared_ptr meets the concept requirements for the implicitly_convertible, so it only requires registering the conversion in the module definition:

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