previous | start | next

## Memory Management for Linked Lists

- When a linked list (Chapter 16) is no longer needed, the nodes should be deleted.
- To automate this task you define a destructor.

  >     List::~List()
  >     {
  >        free();
  >     }

  >     void List::free()
  >     {
  >        while(begin() != end())
  >           erase(begin());
  >     }

- The free function is kept separate from the destructor because its implementation is also needed for the assignment operator.

  >     List& List::operator=(const List& b)
  >     {
  >        if (this != &b)
  >        {
  >           free();
  >           copy(b);
  >        }
  >        return *this;
  >     }

previous | start | next