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



 template <class T>
 class Stack
    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 ; } 
    int size ;  
    int top ;  
    T* stackPtr ;  
 } ;
9/8/2009 6:23:52 PM

Accepted Answer

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

Here's a description from

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.


2/15/2017 9:12:19 AM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow