Memory ◾ 101
C++ classes must dene a destructor when any class method (usually
the constructor) allocates heap memory that is retained by an object. e
destructor is a special function that has no return type (not even void)
and bears the same name as the class, preceded by ‘~’. e compiler implic-
itly invokes the destructor when objects go out of scope or when the delete
operator is called. us, the class destructor is poised to deallocate any
heap memory before its handle (an object data member) goes out of scope.
A destructor is appropriately dened in Example 4.5.
Classes must also dene a copy constructor when objects allocate
heap memory. Why? e compiler automatically provides a default copy
constructor that simply copies each ...