Latest

recent

General Template Friend Class or Function in C++


It would be helpful to add a display operator to the Array class. One approach would be to declare a display operator for each possible type of Array, but this would undermine the whole point of having made Array a template.
What is needed is an insertion operator that works for any possible type of Array.
ostream& operator<< (ostream& Array<T>&);
To make this work, we need to declare operator<< to be a template function.
template <class T> ostream& operator<< (ostream&, Array<T>&)
Now that operator<< is a template function, you need only to provide an implementation. Listing 19.4 shows the Array template extended to include this declaration and provides the implementation for the operator<<.

NOTE: To compile this listing, copy lines 8-26 of Listing 19.2 and insert them between lines 3 and 4. Also copy lines 51-86 of Listing 19.2 and insert them between lines 37 and 38.

Listing 19.4. Using operator ostream.

1: #include <iostream.h>
2:
3: const int DefaultSize = 10;
4:
5: template <class T> // declare the template and the parameter
6: class Array // the class being parameterized
7: {
8: public:
9: // constructors
10: Array(int itsSize = DefaultSize);
11: Array(const Array &rhs);
12: ~Array() { delete [] pType; }
13:
14: // operators
15: Array& operator=(const Array&);
16: T& operator[](int offSet) { return pType[offSet]; }
17: const T& operator[](int offSet) const
18: { return pType[offSet]; }
19: // accessors
20: int GetSize() const { return itsSize; }
21:
22: friend ostream& operator<< (ostream&, Array<T>&);
23:
24: private:
25: T *pType;
26: int itsSize;
27: };
28:
29: template <class T>
30: ostream& operator<< (ostream& output, Array<T>& theArray)
31: {
32: for (int i = 0; i<theArray.GetSize(); i++)
33: output << "[" << i << "] " << theArray[i] << endl; return output;
34: }
35:
36: enum BOOL { FALSE, TRUE};
37:
38: int main()
39: {
40: BOOL Stop = FALSE; // flag for looping
41: int offset, value;
42: Array<int> theArray;
43:
44: while (!Stop)
45: {
46: cout << "Enter an offset (0-9) ";
47: cout << "and a value. (-1 to stop): " ;
47: cin >> offset >> value;
48:
49: if (offset < 0)
50: break;
51:
52: if (offset > 9)
53: {
54: cout << "***Please use values between 0 and 9.***\n";
55: continue;
56: }
57:
58: theArray[offset] = value;
59: }
60:
61: cout << "\nHere's the entire array:\n";
62: cout << theArray << endl;
63: return 0;
64: }


Output: Enter an offset (0-9) and a value. (-1 to stop): 1 10
Enter an offset (0-9) and a value. (-1 to stop): 2 20
Enter an offset (0-9) and a value. (-1 to stop): 3 30
Enter an offset (0-9) and a value. (-1 to stop): 4 40
Enter an offset (0-9) and a value. (-1 to stop): 5 50
Enter an offset (0-9) and a value. (-1 to stop): 6 60
Enter an offset (0-9) and a value. (-1 to stop): 7 70
Enter an offset (0-9) and a value. (-1 to stop): 8 80
Enter an offset (0-9) and a value. (-1 to stop): 9 90
Enter an offset (0-9) and a value. (-1 to stop): 10 10
***Please use values between 0 and 9.***
Enter an offset (0-9) and a value. (-1 to stop): -1 -1

Here's the entire array:
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40
[5] 50
[6] 60
[7] 70
[8] 80
[9] 90

Analysis: On line 22, the function template operator<<() is declared to be a friend of the Array class template. Because operator<<() is implemented as a template function, every instance of this parameterized array type will automatically have an operator<<(). The implementation for this operator starts on line 29. Every member of an array is called in turn. This only works if there is an operator<< defined for every type of object stored in the array.
General Template Friend Class or Function in C++ Reviewed by 1000sourcecodes on 23:49 Rating: 5
Powered by Blogger.