| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | // frnd2tmp.cpp -- template class with non-template friends#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 parameter};// each specialization has its own static data membertemplate <typename T>int HasFriend<T>::ct = 0;// non-template friend to all HasFriend<T> classesvoid counts(){    cout << "int count: " << HasFriend<int>::ct << "; ";    cout << "double count: " << HasFriend<double>::ct << endl;}// non-template friend to the HasFriend<int> classvoid reports(HasFriend<int> & hf){    cout <<"HasFriend<int>: " << hf.item << endl;}// non-template friend to the HasFriend<double> classvoid reports(HasFriend<double> & hf){    cout <<"HasFriend<double>: " << hf.item << endl;}int main(){    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);    // std::cin.get();    return 0; }
 |