Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Cython Programming (Second Edition)

You're reading from   Learning Cython Programming (Second Edition) Expand your existing legacy applications in C using Python

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher Packt
ISBN-13 9781783551675
Length 110 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Philip Herron Philip Herron
Author Profile Icon Philip Herron
Philip Herron
Arrow right icon
View More author details
Toc

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&apos...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image