/* example/example12.cpp -- overriding virtual functions from Python Copyright (c) 2015 Wenzel Jakob All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #include "example.h" #include /* This is an example class that we'll want to be able to extend from Python */ class Example12 { public: Example12(int state) : state(state) { cout << "Constructing Example12.." << endl; } ~Example12() { cout << "Destructing Example12.." << endl; } virtual int run(int value) { std::cout << "Original implementation of Example12::run(state=" << state << ", value=" << value << ")" << std::endl; return state + value; } virtual void pure_virtual() = 0; private: int state; }; /* This is a wrapper class that must be generated */ class PyExample12 : public Example12 { public: using Example12::Example12; /* Inherit constructors */ virtual int run(int value) { /* Generate wrapping code that enables native function overloading */ PYBIND_OVERLOAD( int, /* Return type */ Example12, /* Parent class */ run, /* Name of function */ value /* Argument(s) */ ); } virtual void pure_virtual() { PYBIND_OVERLOAD_PURE( void, /* Return type */ Example12, /* Parent class */ pure_virtual /* Name of function */ /* This function has no arguments */ ); } }; int runExample12(Example12 *ex, int value) { return ex->run(value); } void runExample12Virtual(Example12 *ex) { ex->pure_virtual(); } void init_ex12(py::module &m) { /* Important: use the wrapper type as a template argument to class_<>, but use the original name to denote the type */ py::class_(m, "Example12") /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */ .alias() .def(py::init()) /* Reference original class in function definitions */ .def("run", &Example12::run) .def("pure_virtual", &Example12::pure_virtual); m.def("runExample12", &runExample12); m.def("runExample12Virtual", &runExample12Virtual); }