It is possible to declare any class or function to be a friend to your template class. Each instance of the class will treat the friend pro...
It is possible to declare any class or function to be a friend to your template class. Each instance of the class will treat the friend properly, as if the declaration of friendship had been made in that particular instance. Listing 19.3 adds a trivial friend function, Intrude(), to the template definition of the Array class, and the driver program invokes Intrude(). Because it is a friend, Intrude() can then access the private data of the Array. Because this is not a template function, it can only be called on Arrays of int.
NOTE: To use Listing 19.3, copy lines 1-26 of Listing 19.2 after line 1 of this listing, and then copy lines 51-86 of Listing 19.2 after line 37 of this listing.
1: // Listing 19.3 - Type specific friend functions in templates
2:
3: template <class T> // declare the template and the parameter
4: class Array // the class being parameterized
5: {
6: public:
7: // constructors
8: Array(int itsSize = DefaultSize);
9: Array(const Array &rhs);
10: ~Array() { delete [] pType; }
11:
12: // operators
13: Array& operator=(const Array&);
14: T& operator[](int offSet) { return pType[offSet]; }
15: const T& operator[](int offSet) const
16: { return pType[offSet]; }
17: // accessors
18: int GetSize() const { return itsSize; }
19:
20: // friend function
21: friend void Intrude(Array<int>);
22:
23: private:
24: T *pType;
25: int itsSize;
26: };
27:
28: // friend function. Not a template, can only be used
29: // with int arrays! Intrudes into private data.
30: void Intrude(Array<int> theArray)
31: {
32: cout << "\n*** Intrude ***\n";
33: for (int i = 0; i < theArray.itsSize; i++)
34: cout << "i: " << theArray.pType[i] << endl;
35: cout << "\n";
36: }
37:
38: // driver program
39: int main()
40: {
41: Array<int> theArray; // an array of integers
42: Array<Animal> theZoo; // an array of Animals
43: Animal *pAnimal;
44:
45: // fill the arrays
46: for (int i = 0; i < theArray.GetSize(); i++)
47: {
48: theArray[i] = i*2;
49: pAnimal = new Animal(i*3);
50: theZoo[i] = *pAnimal;
51: }
52:
53: int j, k;
54: for (j = 0; j < theArray.GetSize(); j++)
55: {
56: cout << "theZoo[" << j << "]:\t";
57: theZoo[j].Display();
58: cout << endl;
59: }
60: cout << "Now use the friend function to ";
61: cout << "find the members of Array<int>";
62: Intrude(theArray);
63:
63: // return the allocated memory before the arrays are destroyed.
64: for (k = 0; k < theArray.GetSize(); k++)
65: delete &theZoo[j];
66:
67: cout << "\n\nDone.\n";
68: return 0;
69: }
Output: theZoo[0]: 0
theZoo[1]: 3
theZoo[2]: 6
theZoo[3]: 9
theZoo[4]: 12
theZoo[5]: 15
theZoo[6]: 18
theZoo[7]: 21
theZoo[8]: 24
theZoo[9]: 27
Now use the friend function to find the members of Array<int>
*** Intrude ***
i: 0
i: 2
i: 4
i: 6
i: 8
i: 10
i: 12
i: 14
i: 16
i: 18
Done.
Analysis: The declaration of the Array template has been extended to include the friend function Intrude(). This declares that every instance of an array will consider Intrude() to be a friend function; thus, Intrude() will have access to the private member data and functions of the array instance.
On line 33, Intrude() accesses itsSize directly, and on line 34 it accesses pType directly. This trivial use of these members was unnecessary because the Array class provides public accessors for this data, but it serves to demonstrate how friend functions can be declared with templates.