What I'm trying to do is create a virtual class (ClassTest) in a C++ program, then import it in Python script, create derived class in that script and then import that derived class back in C++.
Here's the code that I came up with so far:
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
using namespace std;
class ClassTest {
public:
ClassTest () {}
virtual int someFunction () = 0;
};
BOOST_PYTHON_MODULE(ClassTest) {
class_< ClassTest, boost::noncopyable >("ClassTest", no_init)
.def("someFunction", &ClassTest::someFunction)
;
}
int main() {
try {
Py_Initialize();
initClassTest();
ClassTest* testObject = **???**
cout << "Function result = " << testObject.someFunction() << endl;
Py_Finalize();
} catch (error_already_set& e) {
PyErr_PrintEx(0);
return 1;
}
return 0;
}
And there's Python script:
import ClassTest
class classTestChild(ClassTest.ClassTest):
def someFunction ():
return 4;
Here is the complete example how this can be done. You need a wrapper class so that virtual functions can be overridden in Python.
ClassTest.h:
ClassTest.cpp (must be built into .so/.dll):
main.cpp (executable):
And the output:
This example will work if ClassTest.so/ClassTest.dll and .exe are in the same folder and .exe is being called from the same folder (./test.exe)