Previous Lecture | Next Lecture | Exercises | Top Level

Code Walkthrough

Contents


Next Section | Contents

The List Class

Simplified version of List.C

In order for a class to provide a reliable service, the class should not depend on what the user does with his data. Therefore, a class should always operate on copies of objects provided by the user.

The List class is a nice example for why this is a good idea. Suppose we would let the List class link in the user's object directly (Left diagram), and the user later decides to modify the object. Perhaps the object will even get destroyed (middle Diagram). This will definitly modify the behaviour of the list, if only that elements suddenly disappear.

We will therefore require that every Listable have a duplicate() member function which will create a copy of itself. Consider this as a virtual copy constructor (right diagram).

normal insert

Let's review the operations necessary for inserting an element:

  // copy is Y, this is X
  copy->next_        = this;
  copy->prev_        = this->prev_;
  this->prev_->next_ = copy;
  this->prev_        = copy;
normal insert
  // copy is Y, this is X
  copy->next_        = this;
  copy->prev_        = this->prev_;
  this->prev_->next_ = copy;
  this->prev_        = copy;

  // Special Case
  if (list_->head_ == this)
    list_->head_ = copy;
head insert


Previous Section | Contents

The Base Class

Simplified version of Base.h and Base.C


Previous Lecture | Next Lecture | Exercises | Contents
Christian Goetze