Tools.h++ Manual

104011 Tandem Computers Incorporated 14-5
14
Here is an example that expands on the Example in “Example” on page 2. The
problem is to push some values onto a stack and then to see if a certain value
exists on the stack (test for "isEqual").
The member function
contains()
of class
RWGStack
(type) has prototype:
RWBoolean contains
(
RWBoolean (*t)(const
type
*, const void*),
const void* a
) const;
The first argument is
RWBoolean (*t)(const
type
*, const void*)
. This
is a pointer to the tester function, for which we will have to provide an
appropriate definition:
#include <rw/gstack.h>
#include <rw/rstream.h>
declare(RWGStack, int)
RWBoolean myTesterFunction(const int* jp, const void* a) // 1
{
return *jp == *(const int*)a; //2}
main()
{
RWGStack(int) gs; // 3
gs.push(new int(1)); // 4
gs.push(new int(2)); // 5
gs.push(new int(3)); // 6
gs.push(new int(4)); // 7
int aValue = 2; // 8
if ( gs.contains(myTesterFunction, &aValue) ) // 9
cout << "Yup.\n";
else
cout << "Nope.\n";
return 0;
}