O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Destructors

The opposite of the constructor, the destructor is used to destroy the object and return any pieces of member data that have separately allocated memory to the system. The destructor is called when the delete keyword is used in conjunction with the variable name.

Having been called, the pointer is set to unassigned, and so the next line of code should assign the pointer to NULL in case it is accessed at a later line of code. The risk is that the program will crash if a line of code tries to use the unassigned pointer.

One use of destructors is in deleting a list. A list will contain a series of nodes, each pointing to the next. Part of the implementation of the destructor might be to delete the node pointed to. In calling this deletion, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required