20
// 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).

Rob
  • 3,333
  • 5
  • 28
  • 71
Milad Sobhkhiz
  • 1,109
  • 4
  • 13
  • 26

7 Answers7

30

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.

Klaim
  • 67,274
  • 36
  • 133
  • 188
Teo Klestrup Röijezon
  • 5,097
  • 3
  • 29
  • 37
27

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
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @Sujoy: what do you mean by that? – Nawaz Mar 17 '11 at 18:30
  • @Nawaz: I mean that in the given question `~` clearly is not used for bitwise negation. – Sujoy Mar 17 '11 at 18:38
  • 5
    @Nawaz "what is the meaning of ~AirlineTicket(); in this code?" is not answered by "bitwise negation", it is a destructor – Daniel DiPaolo Mar 17 '11 at 18:40
  • @Sujoy: If you read my answer carefully, then I didn't say that either. – Nawaz Mar 17 '11 at 18:40
  • @Nawaz: and i said that it is "not in this context" – Sujoy Mar 17 '11 at 18:41
  • 4
    @Daniel: That is not the question. The question is : what is the meaning of `~` in C++?. And the OP sees it's usage in one context, I gave him other context where the same sign is used with entirely different meaning! – Nawaz Mar 17 '11 at 18:42
  • 3
    @Nawaz that is *literally* the question, I am quoting the asker, they're asking what it means **in this code** and your answer doesn't address that – Daniel DiPaolo Mar 17 '11 at 18:44
  • 6
    @Nawaz this is not a place to dump random related information, answers are supposed to address the question being asked, yours does not - regardless of whatever caveats you put in your explanation – Daniel DiPaolo Mar 17 '11 at 18:49
  • 3
    @Daniel: It's not RANDOM. It's the SAME SIGN. If I were to talk about design patterns or politics, it would have been RANDOM. It seems, for you things are either same or random, but not somehow related! – Nawaz Mar 17 '11 at 18:50
  • 1
    I'll agree it's not random, but bitwise negation on signed integers depending on the integers representation, I would have preferred a better example... – Matthieu M. Mar 17 '11 at 19:14
  • @Matthieu: Feel free to edit my post if you've better example. Hopefully, I learn something from it. :-) – Nawaz Mar 17 '11 at 19:17
  • 11
    +1 It's important that this answer is on this page, or the OP might have gone away thinking that the tilde -- which he's _never seen before in C++ -- is only used as part of a destructor's name. And that's just not true. (Though in order to make it a really decent answer, you ought to have spoken about destructors, too.) – Lightness Races in Orbit May 09 '11 at 15:37
  • 2
    @Tomalak: That's very repetitive, as almost everyone has spoken about destructor. So I wrote what others didn't. – Nawaz May 09 '11 at 15:46
  • @Nawaz: Answers should be self-contained, no? Stack Overflow is not a message board. – Lightness Races in Orbit May 09 '11 at 15:51
  • 1
    @Tomalak: But it makes most part of the answer repetitive and so uninteresting. Readers might not be interested to read the complete post. If that is so, then I don't see any advantage by making it "self-contained". – Nawaz May 09 '11 at 15:55
  • 4
    +1 Came in from a search looking for the meaning of tilde in C++ code I was examining. In my case it *was* a bitwise operation. Having both answers here was helpful. – BryKKan Jun 11 '18 at 17:43
7

~ signs that it is a destructor and when ever the object goes out of scope, corresponding destructor is called.

When the destructor is called ?

Taking an example -

#include <iostream> 
class foo
{
    public:
    void checkDestructorCall() ;
    ~foo();
};

void foo::checkDestructorCall()
{
    foo objOne;   // objOne goes out of scope because of being a locally created object upon return of checkDestructorCall
}

foo:: ~foo()
{
    std::cout << "Destructor called \t" << this << "\n";
}

int main()
{
    foo obj;    // obj goes of scope upon return of main
    obj.checkDestructorCall();
    return 0;
}

Results on my system:

Destructor called   0xbfec7942  
Destructor called   0xbfec7943

This example just serves to indicate when a destructor is called. But destructor is written only when the class manages resources.

When class manages resources?

#include <iostream> 
class foo
{

    int *ptr;

    public:
    foo() ; // Constructor
    ~foo() ;

};

foo:: foo()
{
     ptr = new int ; // ptr is managing resources.
                     // This assignment can be modified to take place in initializer lists too.
}

foo:: ~foo()
{
    std::cout << "Destructor called \t" << this << "\n";
    delete ptr ; // Returning back the resources acquired from the free store.
                 // If this isn't done, program gives memory leaks.
}

int main()
{
    foo *obj = new foo;
    // obj is pointing to resources acquired from free store. Or the object it is pointing to lies on heap. So, we need to explicitly call delete on the pointer object.

    delete obj ;  // Calls the ~foo
    return 0;
}

Results on my system:

Destructor called   0x9b68008

And in the program, you posted I don't see a need to write destructor. Hope it helps !

Mahesh
  • 34,573
  • 20
  • 89
  • 115
4

It's the class destructor. You might want to pick up an introductory text on C++ development, as destructors are a fundamental concept in OOP. There is a good reference site here, and the C++ FAQ is another good resource.

Glenn McAllister
  • 1,813
  • 17
  • 14
  • 4
    Better yet, check out the [Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Fred Larson Mar 17 '11 at 18:31
2

~AirlineTicket(); is the destructor for the class AirlineTicket

see this link for further reference

The destructor is called when you delete the object of that class, to free any memory allocated or resources being used by the object.

Sujoy
  • 8,041
  • 3
  • 30
  • 36
1

~ introduces a destructor. It's used because (a) it was available, ad (b) ~ is one (of several) mathematical notation for "not".

geekosaur
  • 59,309
  • 11
  • 123
  • 114