I have two virtual classes I would like to wrap in boost python, I want to be able to write Python class that extends them. The thing is, one of the classes has a method that return a reference of the other class, and I can’t figure how to do.
Here is a simplified version of code of class to be wrapped.
class Foo
{
public:
virtual ~Foo() {}
virtual int a() = 0;
};
class Bar
{
public:
virtual ~Bar() {}
virtual Foo const& b() = 0;
};
So I started wrapping about this way.
class FooWrap : public Foo, public wrapper<Foo>
{
public:
int a()
{
return this->get_override("a")();
}
};
class BarWrap : public Bar, public wrapper<Bar>
{
public:
Foo const& b()
{
return this->get_override("b")();
}
};
BOOST_PYTHON_MODULE(foobar)
{
class_<FooWrap, boost::noncopyable>("Foo")
.def("a", pure_virtual(&Foo::a))
;
class_<BarWrap, boost::noncopyable>("Bar")
.def("b", pure_virtual(&Bar::b))
;
}
And I get a compile error about “cannot instantiate abstract class […]
pure virtual function was not defined” “see declaration of ‘foo::a'”
I’ve been able to compile and run your code after I added call policy for
Bar::bfunction:Basically, it’s just means that lifetime of returned reference from
Bar::bshould be dependent on lifetime ofBarinstance. You can read about call policies in boost docs.What compiler and boost version are you using? I’ve got the following descriptive error with boost 1.46.0 and gcc 4.6.1: