Previous Exercise | Next Exercise | Lecture | Top Level

Pointers

Context

We are now approaching the end of the tool building phase. Our choice of tools is now heavily biased in favor of the application we are building.

In fact, we are going to take advantage of a feature of boardgames in general, and monopoly in particular: The type and number of components doesn't change during a game.

On the other hand, components move around in the game a lot. Components will refer to other components:

Since some components will get fairly large, we will not move around the components themselves, but pointers to the components.

For every type of component, we will therefore keep one reference list of all their instantiations. This list will be a derivative of the Index class. We will implement those lists starting next week.

relations

This week, we will concentrate on implementing the Pointer class and on cleaning up our code somewhat.

Exercises

Change 8
Implement the Pointer class. As usual, the header file and the tests are provided, you have to code the implementation.

The pointer class will contain a member of the Key class. This will be the only data element to be read and written.

The other data element contained in the pointer class is a pointer to an Index, in which we will search for an element that has the same Key as the one stored in the pointer class. This element is initialized by the constructor. If you wonder why we aren't reading this data element, think of what kind of information this data element contains. The information is already known at compile time. Whoever uses a variable of the pointer class will know to which index this pointer will be refering to. The only thing we don't know is to which element the pointer is refering to.

If you wonder why we aren't overloading operator->(), the answer is: "not yet". The pointer class will serve as basis from which we will derive classes pointing to specific lists. These classes will overload operator->() (and operator*() and operator[]()). You will then be able to use our smart pointers the same way as you are used to using C pointers.

Note that the Pointer class is derived from Listable, so that we can create lists of pointers and assign a usefull meaning to operator++() and operator--(), later in the project.

Change 9
Examine once again Base.h and Base.C. You will notice a forward declaration of Game. Inside the class definition, you will find a static pointer to a game object. This means that all objects you create will have a pointer to the current game.

Also note that the static pointer is neither private nor public. It's protected. This means that only member functions of derivatives of Base may access this pointer. This effectively protects our game data from being tampered by non-member functions, while still allowing member functions to access this data. Note that this is much better technique than declaring a global variable

This pointer will be set by the Game constructor. Refer to the way Indexable initializes its base class. Add a constructor to the Game class initializing Base the same way.

Do not forget to reset the game pointer to 0 when the game object is destroyed. This way, other objects will notice if the game has been yanked away from underneath...

Change 10
Now is the occasion to have someone else in your project team look over your code. Try to tighten it, optimize it and clean it up. You might consider adding some comments, too. The main point of this change is to have someone else than your "usual suspect" look at the code and pass a full regression test, just to make sure everything works. Note that you will have to change at least one file.

Instructors will verify that the person who has developed the least number of changes is doing this one.

Change 11
For the final change before we start the game engine proper, we will delete all tests. We will start with fresh tests next week. Use the following commands to remove all tests:

% aerm baseline/test/*.C baseline/test/00/*
In the lecture, I described the const keyword. Now is the time to use it and to get rid of all those compiler warnings. Please do not attempt to change anything else in your code except for adding const keywords. Instructors will reject changes that modify anything else. Instructors will also reject changes that will produce compiler warnings, or that are missing const keywords in essential places.


Christian Goetze