I use Boost.Python to expose my C ++ code in Python. I ran into difficulties associated with the fact that an object was transferred from one language to another several times. Here is what I want to do:
C ++ code
class Base { public: void baseTest() { std::cout << "Base::basetest()"; } }; class Deriv : public Base { public: void derivTest() { std::cout << "Deriv::derivTest()"; } }; void call(Base& b, boost::python::object func) { func(b); } BOOST_PYTHON_MODULE(event) { using namespace boost; using namespace boost::python; class_<Base>("Base") .def("baseTest", &Base::baseTest) ; class_<Deriv, bases<Base>>("Deriv") .def("derivTest", &Deriv::derivTest) ; def("call", call); }
Python code
from event import * def callback(deriv): deriv.baseTest()
What i want to do
C ++: calls the run() function defined in Python. (No problem here)
Python: run() creates a new Deriv object; it passes it and the object to the callback function, back to C ++, through the call function. (Also good)
C ++: call() takes a Deriv object as Base& . It passes the basic Python callback reference received by the second parameter.
Python: callback gets Base object from C ++. However, he expects it to be Deriv : if I call derivTest() , the program will derivTest() . However, if I call baseTest() , it does not crash.
How to make a callback not emergency?
source share