C++ new and del keyword
Cython understands the new
keyword from C++; so, consider that you have a C++ class:
class Car { int doors; int wheels; public: Car (); ~Car (); void printCar (void); void setWheels (int x) { wheels = x; }; void setDoors (int x) { doors = x; }; };
It is defined in Cython as follows:
cdef extern from "cppcode.h" namespace "mynamespace":
cppclass Car:
Car ()
void printCar ()
void setWheels (int)
void setDoors (int)
Note that we do not declare the ~Car
destructor because we never call this directly. It's not an explicitly callable public member; this is why we never call it directly but delete will and the compiler will ensure this is called when it will go out of scope on the stack. To instantiate the raw C++ class in Cython code on the heap, we can simply run the following:
cdef Car * c = new Car ()
You can then go and use del
to delete the object at any time using Python's del
keyword:
del c
You will see...