[C++-sig] SWIG: overriding virtual functions in Python.

Jacek Generowicz jacek.generowicz at cern.ch
Tue Apr 15 16:10:59 CEST 2003


given a C++ class

  struct foo {
    virtual void one() {
      cout << "This is foo::one() calling two()" << endl;
      two();
    }
    virtual void two() {
      cout << "This is foo::two()" << endl;
    }
  };

I would like to subclass it in Python and override "two()" thus:

  class bar(foo):
    def two(self):
      print "This is bar.two()"

in such a way that calling bar.one() ...

  bar().one()

gives the result

  This is foo::one() calling two()
  This is bar.two()

rather than 

  This is foo::one() calling two()
  This is foo.two()

In Boost this is achieved by doing providing a wrapper class around
the original C++ implementation, and giving it a default
implementation of the virtual function, which calls back into Python.

Is there a way of doing it in SWIG?





More information about the Cplusplus-sig mailing list