Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of "~" (tilde) symbol in C++?

Tags:

c++

// AirlineTicket.h

class AirlineTicket
{
public:
  AirlineTicket();

  ~AirlineTicket();
     
  int          getNumberOfMiles();
    
private:
  int          mNumberOfMiles;
};

I want now what is the meaning of ~AirlineTicket(); in this code? I don't know the meaning of ~ (tilde).

like image 369
Milad Sobhkhiz Avatar asked Mar 17 '11 18:03

Milad Sobhkhiz


2 Answers

It is the destructor. It gets called when you destroy (reaching end of scope, or calling delete to a pointer to) the instance of the object.

like image 180
Teo Klestrup Röijezon Avatar answered Oct 09 '22 17:10

Teo Klestrup Röijezon


In the context you're using it, it defines a destructor.

In other context such as the following one, it's also called bitwise negation (complement):

int a = ~100;
int b = ~a;

Output: (ideone)

-101
100
like image 42
Nawaz Avatar answered Oct 09 '22 17:10

Nawaz