Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

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.

//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, the macro serves as a restricted interface to the underlying implementation.
//2 File <rw/rstream.h> is a special Rogue Wave header file that includes <iostream> and any other stream-related adjustments related to your compiler.
//3 This 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.
//4 At this line an instance gs of the new class RWGStack(int) is created.
//5-//8  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.
//9  The member function entries() of class RWGStack(int) is called to verify how many items are on the stack.
//10  A pointer to an int is declared and defined.
//11  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.
//12  Each item is dereferenced and printed.


Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, 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.