// frnd2tmp.cpp -- ÅÛÇø´ÀÌ ¾Æ´Ñ ÇÁ·»µå¸¦ °¡Áö´Â ÅÛÇø´ Ŭ·¡½º #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 report(HasFriend &); // ÅÛÇø´ ¸Å°³º¯¼ö }; // °¢ Ư¼öÈ­´Â ÀڽŸ¸ÀÇ static µ¥ÀÌÅÍ ¸â¹ö¸¦ °¡Áø´Ù template int HasFriend::ct = 0; // ¸ðµç HasFriend Ŭ·¡½º¿¡ ´ëÇØ ÅÛÇø´ÀÌ ¾Æ´Ñ ÇÁ·»µå void counts() { cout << "int Ä«¿îÆ®: " << HasFriend::ct << "; "; cout << "double Ä«¿îÆ®: " << HasFriend::ct << endl; } // HasFriend Ŭ·¡½º¿¡ ´ëÇØ ÅÛÇø´ÀÌ ¾Æ´Ñ ÇÁ·»µå void report(HasFriend & hf) { cout <<"HasFriend: " << hf.item << endl; } // HasFriend Ŭ·¡½º¿¡ ´ëÇØ ÅÛÇø´ÀÌ ¾Æ´Ñ ÇÁ·»µå void report(HasFriend & hf) { cout <<"HasFriend: " << hf.item << endl; } int main() { cout << "¼±¾ðµÈ °´Ã¼ ¾øÀ½: "; counts(); HasFriend hfi1(10); cout << "hfi1 ¼±¾ð ÈÄ: "; counts(); HasFriend hfi2(20); cout << "hfi2 ¼±¾ð ÈÄ: "; counts(); HasFriend hfdb(10.5); cout << "hfdb ¼±¾ð ÈÄ: "; counts(); report(hfi1); report(hfi2); report(hfdb); return 0; }