4

Say I have a simple C++ class

class MyClass{
     //constructor
     MyClass(int value):_value(value){};

     void operator()(AnotherClass& const b){
     // Do something with b object
     }

    private:
      int _value;
}

I am trying to create Python binding of this class using pybind11. How can I bind the operator() method?

This binding is going to be used to pass objects of this class to functions requiring a callback function as an argument.

1 Answer 1

7

I figured this out after trying various things. We need to define the __call__ method in our binding. As an example:

.def("__call__", [](MyClass& this, AnotherClass& const b){
                   return this(b);
                 }
    )
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.