Освой самостоятельно С++ за 21 день.
Шрифт:
106: Animal *pAnimal;
107:
108: // заполняем массивы
109: for (int i = 0; i < theArray.GetSize; i++)
110: {
111: theArray[i] = i*2;
112: pAnimal = new Animal(i*3);
113: theZoo[i] = *pAnimal;
114: }
115:
116: int j;
117: for (j = 0; j < theArray.GetSize; j++)
118: {
119: cout << "theZoo[" << j << "]:\t";
120: theZoo[j].Display;
121: cout << endl;
122: }
123: cout << "Now use the friend function to";
124: cout << "find the members of Array<int>";
125: Intrude(theArray);
126:
127: cout << "\n\nDone.\n";
128: return 0;
129: }
Результат:
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.
Анализ:
В строке 60 функция lntrude непосредственно обращается к члену itsSize, а в строке 61 получает прямой доступ к переменной-члену pType. В данном случае без использования функции-друга можно было бы обойтись, поскольку класс Array предоставляет открытые методы доступа к этим данным. Этот листинг служит лишь примером того, как можно объявлять и использовать функции-друзья шаблонов.
Дружественный класс или функция как общий шаблон
В класс Array было бы весьма полезно добавить оператор вывода данных. Это можно сделать путем объявления оператора вывода для каждого возможного типа массива, но такой подход свел бы не нет саму идею использования класса Array как шаблона.
Поэтому нужно найти другое решение. Попробуем добиться того, чтобы оператор вывода работал независимо от типа экземпляра массива.
ostream& operator<< (ostream&, Array<T>&);
Чтобы этот оператор работал, нужно так объявить operator<<, чтобы он стал функцией шаблона:
template <class T> ostream& operator<< (ostream&, Array<T>&)
Теперь operator<< является функцией шаблона и его можно использовать в выполнении класса. В листинге 19.4 показано объявление шаблона Array, дополненное объявлением функции оператора вывода operator<<.
Листинг 18.4. Использование оператора вывода
1: #include <iostream.h>
2:
3: const int DefaultSize = 10;
4:
5: class Animal
6: {
7: public:
8: Animal(int);
9: Animal;
10: ~Animal { }
11: int GetWeight const { return itsWeight; }
12: void Display const { cout << itsWeight; }
13: private:
14: int itsWeight;
15: };
16:
17: Animal::Animal(int weight):
18: itsWeight(weight)
19: { }
20:
21: Animal::Animal:
22: itsWeight(0)
23: { }
24:
25: template <class T> // объявляем шаблон и параметр
26: class Array // параметризованный класс
27: {
28: public:
29: // конструкторы
30: Array(int itsSize = DefaultSize);
31: Array(const Array &rhs);
32: ~Array { delete [] pType; }
33:
34: // операторы
35: Array& operator=(const Array&);
36: T& operator[](int offSet) { return pType[offSet]; }
37: const T& operator[](int offSet) const
38: { return pType[offSet]; }
39: // методы доступа
40: int GetSize const { return itsSize; }
41:
42: friend ostream& operator<< (ostream&, Array<T>&);
43:
44: private:
45: T *pType;
46: int itsSize;
47: };
48:
49: template <class T>
50: ostream& operator<< (ostream& output, Array<T>& theArray)
51: {
52: for (int i = 0; i<theArray.GetSize; i++)
53: output << "[" << i << "] " << theArray[i] << endl; return output;
54: }
55:
56: // Ряд выполнений...
57:
58: // выполнение конструктора
59: template <class T>
60: Array<T>::Array(int size):
61: itsSize(size)
62: {
63: pType = new T[size];
64: for (int i = 0; i<size; i++)
65: pType[i] = 0;
66: }
67:
68: // конструктор-копировщик
69: template <class T>
70: Array<T>::Array(const Array &rhs)
71: {
72: itsSize = rhs.GetSize;
73: pType = new T[itsSize];
74: for (int i = 0; i<itsSize; i++)
75: pType[i] = rhs[i];
76: }
77:
78: // перегрузка оператора присваивания (=)
79: template <class T>
80: Array<T>& Array<T>::operator=(const Array &rhs)
81: {
82: if (this == &rhs)
83: return *this;
84: delete [] pType;
85: itsSize = rhs.GetSize;
86: pType = new T[itsSize];
87: for (int i = 0; i<itsSize; i++)
88: pType[i] = rhs[i];
89: return *this;
90: }