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

0

7 Answers 7

29

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.

2
  • 1
    Or when deletion automatically occurs.
    – justkt
    Mar 17, 2011 at 18:22
  • 17
    Note that ~ can also denote bitwise not in a different context.
    – user418748
    Mar 17, 2011 at 18:25
25

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
16
  • 5
    @Nawaz "what is the meaning of ~AirlineTicket(); in this code?" is not answered by "bitwise negation", it is a destructor Mar 17, 2011 at 18:40
  • 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, 2011 at 18:42
  • 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 Mar 17, 2011 at 18:49
  • 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.) May 9, 2011 at 15:37
  • 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, 2018 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 !

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.

1
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.

1

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

1

This indicates destructor of class.

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr380.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.