[C++-sig] Fwd: passing pointers from python

hitesh dhiman hitesh.dhiman.1988 at gmail.com
Fri Mar 19 05:59:07 CET 2010


Hi Jim
Thanks...thats what i'm trying to do right now. Will post if any problems
occur.



On Fri, Mar 19, 2010 at 11:22 AM, Jim Bosch <talljimbo at gmail.com> wrote:

> On Fri, 2010-03-19 at 10:56 +0800, hitesh dhiman wrote:
> >
> >
> > ---------- Forwarded message ----------
> > From: hitesh dhiman <hitesh.dhiman.1988 at gmail.com>
> > Date: Thu, Mar 18, 2010 at 11:39 AM
> > Subject: passing pointers from python
> > To: Development of Python/C++ integration <cplusplus-sig at python.org>,
> > boost-users at lists.boost.org
> >
> >
> > Hi all,
> > i'm trying to wrap c++ functions that have pointer variables. The test
> > example i'm using is shown below:
> > int World::addition(int* a, int* b)
> > {
> > int z = *a + *b;
> > return z;
> > }
> >
> >
> > Now, i defined it in my wrapper as :
> > .def("addition", &World::addition)
> >
> >
> > My code compiles, but when I try to execute it from python, i get this
> > error:
> > Python Argument types in World.Addition(World, int, int) did not match
> > C++ signature: addition(class World {lvalue}, int* , int*)
> >
> >
> > Is there a workaround for this??
> >
>
> I can't think of an easy one - your C++ function signature implies that
> it could change the arguments (otherwise, why pass by pointer?), but
> Python integers are immutable objects, and can't be changed when passed
> as arguments.  That's why Boost.Python isn't doing the conversion
> automatically.
>
> If your code doesn't need to actually modify its arguments - as is the
> case in your example - you can of course write C++ wrappers that take
> arguments by value and forward them as pointers, and just wrap those
> functions for Python.  Note that you can wrap a free function that takes
> a World& as its first argument as a member function of the World class;
> there will be no difference between that and wrapping a true member
> function on the Python side.
>
>
> Jim Bosch
>
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Regards,
Hitesh Dhiman
Electrical Engineering
National University of Singapore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100319/5bb16b55/attachment.html>


More information about the Cplusplus-sig mailing list