Lecture 4 - Slide 12 : 40 |
An example where it is necessary to program a destructor
For sake of the example we introduce a dynamically allocated representation of points
![]() | Implementation of class point - with an insufficient destructor . |
![]() | A sample program with memory leaks. |
![]() | Implementation of class point - now with a destructor that deallocates the Point representation. |
![]() | A sample program - now without memory leaks. |
![]() | Output from the program. |
If an object o allocates resources when constructed - or during its life time - o's destructor is responsible for relinquishing these resources when o is deleted or goes out of scope
If o does not allocate such resources, its destructor is typcially not programmed