Hi,
I can't seem to get inheritance and virtual functions to work together
in a simple python binding:
#include
using namespace boost::python;
struct Base
{
virtual ~Base() { }
void nonVirtualFunc() { }
virtual void virtualFunc() { }
};
struct Derived : public Base
{
~Derived() { }
void virtualFunc() { }
};
struct BaseWrap: public Base, public wrapper<Base>
{
void virtualFunc()
{
if (override virtualFunc = this->get_override("virtualFunc"))
virtualFunc();
else
Base::virtualFunc();
}
void default_virtualFunc() { this->Base::virtualFunc(); }
};
BOOST_PYTHON_MODULE(test)
{
class_("__Base")
.def("nonVirtualFunc", &Base::nonVirtualFunc)
;
class_("Base")
.def("virtualFunc", &Base::virtualFunc, &BaseWrap::default_virtualFunc)
;
class_("Derived")
.def("virtualFunc", &Derived::virtualFunc)
;
}
Now in my python interpreter:
import test
dir(test.__Base)
[<built-ins>, 'nonVirtualFunc']
dir(test.Base)
[<built-ins>, 'virtualFunc']
dir(test.Derived)
[<built-ins>, 'virtualFunc']
Without using BaseWrap to do the virtual functions:
dir(test.Derived)
[<built-ins>, 'nonVirtualFunc', 'virtualFunc']
It seems that by using the boost python wrapper, I now lose any
inherited functions, and the nonVirtualFunc does not get defined in the
subclasses. I may be going about this in the wrong way, but is there a
way of using inheritance and virtual functions together?
Thanks,
Dan