SWIG and C++

Toby J Sargeant tjs at mail.csse.monash.edu.au
Thu Feb 8 06:31:29 EST 2001


On Thu, Feb 08, 2001 at 11:02:27AM +0100, Pierre Puiseux wrote:
> 
> Hello,
> 
> is it possible with SWIG, to import in python a C++ class with 2 methods
> WITH THE SAME NAME but with different arguments.For instance :
> //file truc.h
> class truc
> {...
>         void mymethod(int, int);
>         void mymethod(float);
> }
> //
> 
> //File : truc.i
> %module truc
> 
> %{
> #include "truc.h"
> %}
> 
> %include truc.h
> //
> 
> //here is the swig command and the error message :
> >swig -c++ -shadow -python truc.i
> >Generating wrappers for Python truc.h : Line 17. Function truc_mymethod (member mymethod) multiply defined (2nd definition ignored).
> 
> Thanks

>From memory, the answer is yes, you can. You might like to look at the sip
files for something like PyQT to find out how, though. I don't know why
what you're doing isn't working.

One caveat to this that affected me when I was playing with PyQT
that may still be true is that if you have two methods that have the same
number of arguments, and all the arguments of the second method are
castable to the types of the arguments in the first, then the sip wrapper
will blindly do the cast, and end up calling the first method, even though
the types of the arguments you pass match the signature of the second.

The particular example I'm referring to is

QWMatrix::map(int,int) vs. QWMatrix::map(float,float)

and the net result was that any floating point values passed to map() were
implicitly cast to ints, rather than using the floating point version.

Toby.




More information about the Python-list mailing list