[C++-sig] Instantiating a Python class from C++?

Peter python at cityofdreams.com
Sat Aug 28 14:47:43 CEST 2004


Another question, and I really hope this doesn't get emailed four
idnetical times like my last question.  My sincere apologies in advance
if it does.

I have a C++ class with a virtual function that I want overridden in
Python.  I expose it to Python in the normal way:

  class_<MyClass>("CPP_Class", no_init)
    .def("vfunc", &MyClass::func);  

And in Python the user derives from this class overrides the virtual
method, e.g.:

  class DerivedClass(CPP_Class):
      def vfunc(self):
          #do stuff in Python

I want to create an instance of "DerivedClass" from C++ and assign it to
the base class ptr and then call the overridden Python function via the
virtual fn defined in the C++ base class.  So my question is; how do I,
from C++, create this instance of DerivedClass (assuming I know that the
user called it DerivedClass)?

Thank you very much for any help,
Peter









More information about the Cplusplus-sig mailing list