|
Boost Users : |
Subject: [Boost-users] [Boost.Python] Using Inheritance *and* Virtual Functions in a Python Binding
From: Dan Bailey (drb_at_[hidden])
Date: 2009-10-20 07:41:11
Hi,
I can't seem to get inheritance and virtual functions to work together
in a simple python binding:
#include <boost/python.hpp>
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, boost::noncopyable>("__Base")
.def("nonVirtualFunc", &Base::nonVirtualFunc)
;
class_<BaseWrap, boost::noncopyable>("Base")
.def("virtualFunc", &Base::virtualFunc, &BaseWrap::default_virtualFunc)
;
class_<Derived, bases<Base> >("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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net