[C++-sig] boost.python problem: 'virtual functions and default implementations' tutorial doesn't work

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Fri Mar 12 09:37:18 CET 2004


The problem comes from a change in the handling of the copy constructor
in Boost.Python. Now, if you want to have a copy contructor for your
class, you have to write a contructor in your wrapper as :

BaseWrapper( PyObject* self_, const Base& copy);

In fact, for every constructor you have, you need to add a first
argument being the PyObject* of the current object.

Pierre

Le ven 12/03/2004 à 05:44, Colin Irwin a écrit :
> I'm having a problem following the tutorial that comes with the
> Boost::Python library, in particular defining virtual functions with default
> implementations.  I've been using this as a test, as I have a set of C++
> classes that I want to be available in Python.  These classes use a
> top-level interface that has callback functions in the style of inheritable
> functions.  I would like to be able to define a Python class, deriving this
> off the C++ top-level interface class and in particular inheriting these
> callback functions to deal with certain events.  I have a test setup as
> follows (which pretty much follows the tutorial example) and I've tried to
> compile it using MSVC .NET 2003 (v7.1) with Boost 1.31.0:
> 
> class Base
> {
>     public:
>         virtual void function();
> };
> 
> class BaseWrapper : public Base
> {
>     public:
>         BaseWrapper(PyObject* self_) :
>             self(self_) { }
>         virtual void function() { call_method<void>(this->self,
> "function"); };
>         void default_function() { Base::function(); };
> 
>     private:
>         PyObject* self;
> };
> 
> BOOST_PYTHON_MODULE(test_module)
> {
> 
>     class_<Base, BaseWrapper>("Base")
>         .def("function", &Base::function, &BaseWrapper::default_function)
>     ;
> }
> 
> When I compile it, I get the following error:
> ..\..\42_External_Units\421_Boost\boost\python\object\value_holder.hpp(137)
> : error C2661: 'BaseWrapper::BaseWrapper' : no overloaded function takes 2
> arguments
> 
> ... (a whole set of 'while compiling...') ...
> 
> test.cpp(35) : see reference to class template instantiation
> 'boost::python::class_<T,X1>' being compiled
> 
> with
> 
> [
> 
> T=Base,
> 
> X1=BaseWrapper
> 
> ]
> 
> 
> 
> I've been able to resolve the error by using the following python module
> definition instead:
> 
> BOOST_PYTHON_MODULE(test_module)
> {
> 
>     class_<Base, BaseWrapper, boost::noncopyable>("Base")
>         .def("function", &Base::function, &BaseWrapper::default_function)
>     ;
> }
> 
> Now this noncopyable is not mentioned in the tutorial file and indeed, I
> don't think it does exactly what I want in this case.  By this I mean it
> prevents copying any of the derived objects in Python doesn't it?
> Where have I gone wrong?  The tutorial file doesn't seem to work as
> expected.  My fix doesn't do what I want it do (I think this is right, if
> I've inadvertently prevented copying).  How should I structure my module
> definition/code?
> 
> TIA
> 
> Colin
> 
> 
> 
> 
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68 





More information about the Cplusplus-sig mailing list