Object Lists
Volume Number: 6
Issue Number: 2
Column Tag: Jörg's Folder
C++ Object Lists 
By Jörg Langowski, MacTutor Editorial Board
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
“C++ object lists in windows”
Our third example application in C++ deals with another common aspect of
Macintosh programming: Documents that have several objects associated and on which
some action has to be performed. One of the best known examples would be a MacDraw
document, where a document can contain (almost) any number of lines, polygons,
rectangles, etc. All these objects have to be drawn when the window is updated, have to
be moved or changed otherwise when the mouse is clicked on them, etc.
The classical example of late binding in object-oriented programming is, in fact,
just this type of program: a window is created that contains a number of objects. We
don’t know which type of objects or how many the window will contain at run time; all
we know is that they must be drawn somehow.
Linked lists and late binding
For this type of problem, one usually creates a linked list of objects which is
associated with the window. New objects can be added to or removed from the list.
Drawing the contents of the window simply means traversing the list from beginning to
end and drawing every object that is found. And here run-time binding comes in: since
we don’t know the exact type of the object myObj that we’ll find at any particular place
in the list, we’ll simply write Draw->myObj and let the run time code decide which
particular drawing method should be used.
The example is illustrated by a piece of code from listing 2:
// 1
void TListDoc::DrawWindow(void)
{ TObjLink* temp;
SetPort(fDocWindow);
EraseRect(&fDocWindow->portRect);
if (fObjList->NumObjs() != 0)
for (temp = fObjList->Header();
temp != nil; temp = temp->GetNext())
{ SysBeep(1); // to let something happen
temp->GetmyObj()->Draw(qd.gray); }
} // DrawWindow
TListDoc (listing 2) is our document class, the window that contains the objects
to be drawn. These objects are contained in a list fObjList of type TObjList (listing 3),
which consists of list elements of type TObjLink. Each list element is an object that
contains two instance variables: a pointer to the next element in the list, fNext, and a
pointer to the element’s contents fmyObj. In our case, fmyObj is an object of type
TDisplObj, with the subclasses TRect, TOval, and TRoundRect. Fig. 1 shows its
structure.
Fig. 1: structure of the TObjList class
The contents of a list element are not accessible directly, and the public function
GetMyObj() is provided for returning a pointer to the object referred to by a
particular list element.
List elements can be easily inserted into and removed from structures such as
TObjList by simply changing the fNext pointers. The AddObj and RemoveObj functions
are provided for this purpose. The list is linear, only one link per list element is
defined. If one wanted to create branched lists like those in Lisp, one would have to
define two links, both of which might alternatively point to other list elements or to
simple objects (‘atoms’). This is not what we want for our list of display objects,