[C++-sig] How to do inheritance in boost.python?
michael kapelko
kornerr at gmail.com
Sun Mar 18 04:20:51 CET 2007
I have this:
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
using namespace std;
class Base {
public:
virtual ~Base() {
}
virtual int f() = 0;
};
class BaseWrap: public Base, public wrapper<Base> {
public:
int f() {
return this->get_override("f")();
}
};
BOOST_PYTHON_MODULE(hello) {
class_<BaseWrap, boost::noncopyable>("Base")
.def("f", pure_virtual(&Base::f))
;
}
Module compiles ok into hello.so.
But then I do in Python:
>>> import hello
>>> base = Base();
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'Base' is not defined
>>> class Derived(Base):
... def f(self):
... return 42;
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'Base' is not defined
What's wrong with the code?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Also, here's a code with not pure virtual func:
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
using namespace std;
class Base {
public:
virtual ~Base() {
}
virtual int f() {
return 0;
}
};
class BaseWrap: public Base, public wrapper<Base> {
public:
int f() {
if (override f = this->get_override("f"))
return f();
return Base::f();
}
int default_f() {
return this->Base::f();
}
};
BOOST_PYTHON_MODULE(hello) {
class_<BaseWrap, boost::noncopyable>("Base")
.def("f", &Base::f, &BaseWrap::default_f)
;
}
And in Python I get the same error that Base isn't defined.
Thanks.
More information about the Cplusplus-sig
mailing list