Sams Teach Yourself C++ in 21 Days

(singke) #1
132: Array<int> *pIntArray = new Array<int>;
133:
134: cout << Array<int>::GetNumberArrays() << “ integer arrays\n”;
135: cout << Array<Animal>::GetNumberArrays();
136: cout << “ animal arrays” << endl << endl;
137:
138: delete pIntArray;
139:
140: cout << Array<int>::GetNumberArrays() << “ integer arrays\n”;
141: cout << Array<Animal>::GetNumberArrays();
142: cout << “ animal arrays” << endl << endl;
143: return 0;
144: }

0 integer arrays
0 animal arrays

1 integer arrays
1 animal arrays

2 integer arrays
1 animal arrays

1 integer arrays
1 animal arrays
The Arrayclass has added the static variable tsNumberArrayson line 74, and
because this data is private, the static public accessor GetNumberArrays()was
added on line 66.
Initialization of the static data is accomplished with a full template qualification, as
shown on lines 77 and 78. The constructors of Arrayand the destructor are each modi-
fied to keep track of how many arrays exist at any moment.
Accessing the static members is the same as accessing the static members of any class:
You can do so with an existing object, as shown on lines 134 and 135, or by using the
full class specification, as shown on lines 128 and 129. Note that you must use a specific
type of array when accessing the static data. One variable exists for each type.

692 Day 19


LISTING19.7 continued

ANALYSIS

OUTPUT

Free download pdf