On 2 November 2010 17:17, Robert Bradshaw <[email protected]> wrote:
> On Sun, Oct 31, 2010 at 8:44 PM, Brett Calcott <[email protected]> 
> wrote:
>> 1. Why must I use a *pointer* in my python class?
>> cdef class PythonVector2d:
>>   cdef Vector2d *obj
>> There is a default constructor, and I can happily declare it on the stack in
>> a function. I'm sure I'm missing something here; is there some danger with
>> allowing this?
>
> It's conceivable that we could allow "stack allocated" members in an
> object in the presence default constructors, but that would require
> modifying the constructor and destructor to manually initialize and
> deconstruct the object when the PyObject* is allocated and reclaimed.

I don't get why you need to manually do it. Doesn't the c++ default
construction just do it for you?

>> 2. How hard would it be to automatically forward member access to a
>> contained cppclass? Consider the following nasty hack:
>> cdef class PythonVector2d:
>>   cdef public float x, y
>>   cdef Vector2d *obj
>>   def __cinit__(self, x, y):
>>     self.x = x
>>     self.y = y
>>     self.obj = <Vector 2d*>x
>> ok. That is a bit nasty (I'm sure someone can tell me lots of reasons why I
>> shouldn't do this).
>
> In reading that code, I'm not sure what your intent is.
>

Nastiness explained: Vector2d is a struct with 2 float members. I
declare 2 float members in the python object, and then get a pointer
to the first of these and cast it to a Vector2d. Assuming that the
struct layout is the same in both of these, then accessing members via
the cython properties will update the same shared bit of memory that
the c++ struct uses.

Brett
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to