[C++-sig] Re: pure virtual methods with pyste

wiedeman at gmx.net wiedeman at gmx.net
Mon Jul 28 18:08:18 CEST 2003


> #include <boost/python.hpp>
> using namespace boost::python;
> 
> struct Abstract {
>   virtual void a() = 0;
> };
> 
> struct AbstractWrap: Abstract
> {
>     AbstractWrap(PyObject* self_): self(self_) {}
>     void a(){
>         call_method<void>(self, "a");
>     }
>     PyObject* self;
> };
> 
> void call(Abstract* a)
   ^^^^^^^^^^^^^^^^^
let's say void call_a(...). Naming the method 'call' results in a Internal
Compiler Error with gcc 2.95.2 and with gcc 3.2 :-)
build/WrapTest.cpp: In function `void init_module_wrap_Test()':
build/WrapTest.cpp:28: no matching function for call to `def(const char[5],
   <unknown type>)'

> {
>     a->a();  
> }
> 
> BOOST_PYTHON_MODULE(test)
> {
>     class_<Abstract, AbstractWrap, boost::noncopyable>("Abstract")
>         .def("a", &Abstract::a)
>     ;
>     def("call", call);
> }
> 
> 
> It works, i.e., we can use instances of derived classes in C++ and we 
> can override a() in Python, but it is not safe, because you can 
> instantiate Abstract, and calling its a method will make Python crash. 
> Adding no_init prevents the class from being instantiated, even in a 
> derived class, so that does not work. There's a way to support both 
> usages at the same time?

Now i see what's going wrong - an infinite recursion if the derived class
doesn't implement a(). I suppose, that there is some way to check this, but
cannot i'm not sure how to achieve it. Thanks for the clarification.

Christoph





More information about the Cplusplus-sig mailing list