Tools.h++ Manual

14-6 104011 Tandem Computers Incorporated
14
Program output:
Yup.
A description of each program line follows.
1. This is the tester function. The first argument is a pointer to the type of
objects in the collection,
ints
in this case. The second argument points
to an object that can be of any type. In this example, it also points to an
int
. Both arguments are declared const pointers — in general the tester
function should not change the value of the objects being pointed to.
2. The second argument is converted from a
const void*
to a const
int*
,
then dereferenced. The result is a
const int
. This is then compared to
the dereferenced first argument, which is also a
const int
. The net
result is that this tester function considers a match to have occurred
when the two
ints
have the same values (i.e., they are equal).
Note – We could have chosen to have identified a particular int (i.e., test for
identity).
3–7. These lines are the same as in “Example” on page 2. A generic stack of
(pointers to) ints is declared and defined, then 4 values are pushed onto
it.
8. This is the value (i.e., "2") that we will look for in the stack.
9. Here the member function
contains()
is called, using the tester
function. The second argument of
contains()
(a pointer to the variable
aValue
) will appear as the second argument of the tester function. The
function
contains()
traverses the entire stack, calling the tester
function for each item in turn, waiting for the tester function to signal a
match. If it does,
contains()
returns
TRUE
, otherwise
FALSE
.
Note – The second argument of the tester function does not necessarily have to
be of the same type as the members of the collection (although it is in the
example above).