22.4. Destructors
As well as a constructor, each class has a destructor method with a name like ~SomeClass. (On US keyboards, the ‘~’ symbol is normally on the upper left-hand corner of your keyboard, but in other parts of the world you’ll find it somewhere else.) We have no choice about the names of the constructor and destructor methods. For any class SomeClass, the constructor is called SomeClass and the destructor is called ~SomeClass.
When a variable goes out of scope its destructor is automatically called. Thus if your program has a global variable, the variable’s destructor is called when your program terminates. If you have a function which has a local variable sctemp in it of type SomeClass, the SomeClass constructor is called when ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access