beautypg.com

HP Integrity NonStop J-Series User Manual

Page 146

background image

}

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";

while(!gs.isEmpty())
delete gs.pop();

return 0;
}

Program Output:

Yup.

A description of each program line follows.

//1

This is the tester function. Note that 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_also an int in this example. Note that 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 const int is compared to the dereferenced
first argument, which is also a const int. The net result is that this tester function
considers a match to occur when the two ints have the same values (i.e., they are
equal). Note that we could also choose to identify a particular int (i.e., test for
identity).

//3 -//7 These lines are the same as in the example in

Chapter 12 (

Example of Generic Collection

Classes

)

. 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.

This manual is related to the following products: