186
 template <class T>
 class Stack
 {
 public:
    Stack(int = 10) ; 
    ~Stack() { delete [] stackPtr ; }  //<--- What does the "~" signify?
    int push(const T&); 
    int pop(T&) ;  
    int isEmpty()const { return top == -1 ; } 
    int isFull() const { return top == size - 1 ; } 
 private:
    int size ;  
    int top ;  
    T* stackPtr ;  
 } ;
Monte Hurd
  • 4,349
  • 5
  • 30
  • 35

6 Answers6

197

It's the destructor, it destroys the instance, frees up memory, etc. etc.

Here's a description from ibm.com:

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

See https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzarg/cplr380.htm

inanutshellus
  • 9,683
  • 9
  • 53
  • 71
95

As others have noted, in the instance you are asking about it is the destructor for class Stack.

But taking your question exactly as it appears in the title:

In c++ what does a tilde “~” before a function name signify?

there is another situation. In any context except immediately before the name of a class (which is the destructor context), ~ is the one's complement (or bitwise not) operator. To be sure it does not come up very often, but you can imagine a case like

if (~getMask()) { ...

which looks similar, but has a very different meaning.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
10

It's a destructor. The function is guaranteed to be called when the object goes out of scope.

Samuel Danielson
  • 5,231
  • 3
  • 35
  • 37
  • 2
    ...if its allocated on the stack. When 'delete' is called on the object if its allocated on the heap. – Jherico Sep 08 '09 at 19:00
  • @Jherico: Actually it's when the keyword "delete" is used. Remember that the keyword "new" invokes "operator new" and then calls the constructor on the address returned so it's perfectly possible to "new" memory on the stack. :) The keyword "delete" calls the destructor and then invokes "operator delete". – Troubadour Sep 08 '09 at 20:53
  • @Jherico or lives at namespace scope or is a static class member or function-local static variable: the static storage duration. In other words: all vairables with automic storage will _automatically_ be destructed when appropriate. Beware of [Static Initialization Order Fiasco](http://stackoverflow.com/a/1005709/85371) – sehe Oct 15 '12 at 07:01
6

This is a destructor. It's called when the object is destroyed (out of life scope or deleted).

To be clear, you have to use ~NameOfTheClass like for the constructor, other names are invalid.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • Old answer... I think I meant that after ~ it could be any name as long a it's the same as the class name and constructor name. – Klaim Jun 26 '11 at 10:07
4

It's the destructor. This method is called when the instance of your class is destroyed:

Stack<int> *stack= new Stack<int>;
//do something
delete stack; //<- destructor is called here;
Henrique de Sousa
  • 5,727
  • 49
  • 55
Pierre
  • 34,472
  • 31
  • 113
  • 192
1

That would be the destructor(freeing up any dynamic memory)

Archmede
  • 1,592
  • 2
  • 20
  • 37
maxfridbe
  • 5,872
  • 10
  • 58
  • 80