What version of Cython are you using? What error are you getting?
- Robert
a._ptr and b._prtr are pointers, and you can't add pointers. Instead,
you need to dereference them, i.e.
r._ptr[0] = a._ptr[0] + b._ptr[0]
(or use cython.operator.dereference). Yeah, it's not as pretty as it could be.
- Robert
Yes, your _set_ function doesn't deallocate self._ptr first.
> not sure what [0] really does and how does
Same as in C or C++, if ptr is a pointer, then ptr[0] is the object
being pointed to. *ptr and ptr[0] mean exactly the same thing (unless,
in C++, one or both have been overridden).
> it work along with del keyword. I'm also curious whether
> there isn't a better way to initialize an instance using ctype
> pointer.
You can set the pointer to null initially, and then set it. (Note that
this makes your API potentially unsafe.)
>What about == operator?
Same, just be sure to dereference first, or you'll be comparing
pointers not the objects they point to.
> And lastly, is creating concrete
> types (like vector3 for vector[3]) the usual approach to deal with C++
> template types? Thank you
Yes, for the moment at least. Note that even in C++ the parameters
must be resolved at compile time.
- Robert
We don't yet support templated functions, you'll have to declare this
the "old style" way.
- Robert
Sorry, you'd only need to del if you were re-assiging self._ptr.
Assigning to self._ptr[0] is just fine to do, don't deallocate first.
I still think (a variant of) your original add function was cleaner,
def __add__(vector3 a, vector3 b):
r = vector3()
r._ptr[0] = a._ptr]0] + b._ptr[0]
return r
- Robert