Tools.h++ Manual
14-2 104011 Tandem Computers Incorporated
14
14.1 Example
Here is an example of using a
RWGStack
(generic stack) to store a set of
pointers to
ints
in a last-in, first-out (LIFO) stack. We will go through it line-
by-line and explain what is happening:
Program output:
Stack now has 4 entries
4
3
2
1
Each program line is detailed below.
1. This
#include
defines the preprocessor macro
RWGStackdeclare
(type). This macro is an elaborate and ugly looking
thing that continues for many lines and describes how a "generic stack"
of objects of type type should behave. Mostly, it serves as a restricted
interface to the underlying implementation, which is a singly-linked list
#include <rw/gstack.h> // 1
#include <rw/rstream.h> // 2
declare(RWGStack, int) // 3
main()
{
RWGStack(int) gs; // 4
gs.push(new int(1)); // 5
gs.push(new int(2)); // 6
gs.push(new int(3)); // 7
gs.push(new int(4)); // 8
cout << "Stack now has " << gs.entries()
<< " entries\n"; // 9
int* ip; // 10
while( ip = gs.pop() ) // 11
cout << *ip << "\n"; // 12
return 0;
}