What is '~' for?

May 15, 2016 at 8:05pm
Write your question here.

1
2
3
WorldCup::~WorldCup() {
// empty...
}



Sometimes I can see this kind of constructor
but I don't see why '~' is needed and what is it for.
May 15, 2016 at 8:13pm
It's not a constructor.

It's the destructor. It's the function that gets called when the object is destroyed, either by going out of scope or by having delete called on it.

It's for doing anything that needs doing when the object lifetime ends. For example, releasing resources.

You should never call it.
Last edited on May 15, 2016 at 8:14pm
May 15, 2016 at 8:15pm
It's called a destructor. It's mainly used for deallocation of memory space if you have any pointer-type variables in your class.

http://lmgtfy.com/?q=c%2B%2B+destructor
May 15, 2016 at 8:41pm
Thank you for the answers!:)
Topic archived. No new replies allowed.