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 ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.