-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrnd2tmp.cpp
More file actions
51 lines (45 loc) · 900 Bytes
/
frnd2tmp.cpp
File metadata and controls
51 lines (45 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//frnd2tmp.cpp
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
class HasFriend
{
private:
T item;
static int ct;
public:
HasFriend (const T & i) : item(i) {ct++;}
~HasFriend() {ct--;}
friend void counts();
friend void reports (HasFriend<T> &);
};
template <typename T>
int HasFriend<T>::ct = 0;
void counts()
{
cout << "int count: " << HasFriend<int>::ct << ";";
cout << "double count: " << HasFriend<double>::ct << endl;
}
void reports (HasFriend<double> & hf)
{
cout << "HasFriend<double>: " << hf.item << endl;
}
int main(void)
{
cout << "No objects declared: ";
counts();
HasFriend<int> hfi1(10);
cout << "After hfi1 declared: ";
counts();
HasFriend<int> hfi2(20);
cout << "After hfi2 declared: ";
counts();
HasFriend<double> hfdb(10.5);
cout << "After hfdb declared: ";
counts();
reports(hfi1);
reports(hfi2);
reports(hfdb);
return 0;
}