-4

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

What does ~ in front of a function mean, in C++:

class list
{
    ...other stuff...
public:
    list();
    ~list();
    void insertFront(const TYPE&);
    TYPE deleteFront();
    void insertRear(const TYPE &);
    int isEmpty() const;
    void traverse() const;
};
Community
  • 1
  • 1
Androme
  • 2,399
  • 4
  • 43
  • 82

2 Answers2

5

It means that the function is the destructor for the class it is defined in. The rest of the name (after ~) must match the name of the class.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
1

It is a destructor.

The destructor is called when your object is destroyed, just like the constructor is called when your object is created.

If you created your object with new, the destructor will be called when you call delete.

If you created your object on the stack, the destructor will be called when your object goes out of scope.

The reason you have it is to do any resource cleanup, notification, or other work that is required before the object disappears. The memory reserved for the object itself will be valid for the entire duration of the destructor function.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
paddy
  • 60,864
  • 6
  • 61
  • 103