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

后端 未结 1 362
小蘑菇
小蘑菇 2020-12-22 05:03

I have a function that takes a std::shared_ptr, and I want to pass an object of Derived type to this function from python. Here\'s my class definitions:

stru         


        
相关标签:
1条回答
  • 2020-12-22 05:57

    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
      
    0 讨论(0)
提交回复
热议问题