Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

7.3 Example

Here is an example that uses an RWGStack, a 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:

Each line of the program is detailed below.

//1This #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, the macro serves as a restricted interface to the underlying implementation.
//2This line invokes the macro declare, which is defined in the header file <generic.h> supplied with your compiler. If called with arguments declare(Class, type), it calls the macro Classdeclare with argument type. In this case, the macro RWGStackdeclare, defined in <rw/gstack.h>, will be called with argument int. In other words, the result of calling the declare(RWGStack, int) macro is to create a new class especially for your program. For all practical purposes, its name is RWGStack(int), a stack of pointers to int s.
//3At this line an instance gs of the new class RWGStack(int) is created.
//4-//7 Four ints are created off the heap and inserted into the stack. After statement 8 executes, a pointer to the int 4 will be at the top of the stack, and a pointer to the int 1 at the bottom.
//8 The member function entries() of class RWGStack(int) is called to verify how many items are on the stack.
//9 A pointer to an int is declared and defined.
//10 The stack is popped until empty. The member function pop() will return and remove a pointer to the item on the top of the stack. If there are no more items on the stack it will return zero, causing the while loop to terminate.
//11 Each item is dereferenced and printed.


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.