// frnd2tmp.cpp -- template class with non-template friends #include using std::cout; using std::endl; template 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 &); // template parameter }; // each specialization has its own static data member template int HasFriend::ct = 0; // non-template friend to all HasFriend classes void counts() { cout << "int count: " << HasFriend::ct << "; "; cout << "double count: " << HasFriend::ct << endl; } // non-template friend to the HasFriend class void reports(HasFriend & hf) { cout <<"HasFriend: " << hf.item << endl; } // non-template friend to the HasFriend class void reports(HasFriend & hf) { cout <<"HasFriend: " << hf.item << endl; } int main() { cout << "No objects declared: "; counts(); HasFriend hfi1(10); cout << "After hfi1 declared: "; counts(); HasFriend hfi2(20); cout << "After hfi2 declared: "; counts(); HasFriend hfdb(10.5); cout << "After hfdb declared: "; counts(); reports(hfi1); reports(hfi2); reports(hfdb); // std::cin.get(); return 0; }