On Sun, May 19, 2013 at 12:53 PM, Jan Domański <
jan...@gmail.com> wrote:
> Hi,
>
> Say Rectangle.h has an intersectRectangles function that, given two
> rectangles, will return a rectangle with the area shared by the two rects.
>
> public:
> ...
>
> void move(int dx, int dy);
>
>
> Rectangle intersectRectangles(Rectangle* rec1, Rectangle* rec2);
>
> How could one wrap this function in cython? I've tried with the following:
>
> def PyIntersectRectangles(PyRectangle r1, PyRectangle r2):
> Rectangle rec = intersectRectangles(deref(r1.thisptr), deref(r2.thisptr))
> pyrec = PyRectangle()
> del pyrec.thisptr
> pyrec.thisptr = &rec
> return pyrec
>
> The code above gives me a core dump, and I think I understand why, but I see
> no clear solution.
rec is returned to the stack. You need to do
pyrec = PyRectangle()
pyrec[0] = intersectRectangles(deref(r1.thisptr),
deref(r2.thisptr)) # are you sure you want to deref here, given that
your new function takes pointers?\
return pyrec
Note that thisptr is now not needed for classes with nullary
constructors unless polymorphism is desired, just let it have a
Rectangle member directly.
- Robert