I have a c++ class with a pure virtual function which I wrap in the following way using Boost Python:
class Model {
virtual double Test() = 0;
};
class ModelWrapper : public Model, public boost::python::wrapper<Model> {
double Test() {
return this->get_override("Test")();
}
};
BOOST_PYTHON_MODULE(mymodule)
{
class_<ModelWrapper, boost::noncopyable>("Model")
.def("Test, pure_virtual(&Model::Test))
;
}
I also have a c++ function that's expecting a Model object as an argument:
double MyFunction(Model& m) {
return m.Test()
}
Now what I would like to do is to make a derived class in Python that overrides the Model::Test function but can still be passed to MyFunction. Something like this:
from mymodule import *
class NewModel(Model):
def Test(self):
return 0.5
m = NewModel()
print(MyFunction(m))
which I would want to print out 0.5 in this case. Instead I get this error:
> ArgumentError: Python argument types in
> mymodule.MyFunction(NewModel) did not match C++ signature:
> MyFunction(Model {lvalue})
I'm not sure what step I'm missing in order for NewModel to be usable where Model is expected. Any insight would be greatly appreciated.
I needed to add 'Model.__init__(self)' to the NewModel __init__ function explicitly. After that it worked fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.