1

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.

1 Answer 1

2

I needed to add 'Model.__init__(self)' to the NewModel __init__ function explicitly. After that it worked fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.