12.11.1.1 定义
friend void report(HasFriend &); // possible?
这是不可以的,因为形参没有指定类型,应该写为 HasFriend<特定类型(模板参数名或特定数据类型)> & 模板类中的static数据:要求每种模板的实例化都有自己的static成员(每种类型一个)
12.11.1.2 举例
HasFriend.h
#pragma once #ifndef HASFRIEND_H_ #define HASFRIEND_H_ #includeusing 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 这两个是重载,类型分别是int和double 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; } #endif
12.11.2.1 定义
定义绑定模板友元函数的三个步骤:
1.在模板类定义之前声明模板函数
2.将模板函数声明为模板类的友元函数
3.为友元函数提供定义
执行时以友元函数的模板为准,友元函数给啥类型,类模板就是啥类型
12.11.2.2 举例
HasFriendT.h
#pragma once #includeusing std::cout; using std::endl; // template prototypes template void counts(); template void report(T&); // template class template class HasFriendT { private: TT item; static int ct; public: HasFriendT(const TT& i) : item(i) { ct++; } ~HasFriendT() { ct--; } friend void counts(); friend void report<>(HasFriendT&); }; template int HasFriendT ::ct = 0; // template friend functions definitions template void counts() { cout << "template size: " << sizeof(HasFriendT ) << "; "; cout << "template counts(): " << HasFriendT ::ct << endl; } template void report(T& hf) { cout << hf.item << endl; }
12.11.3.1 定义
友元模板函数的参数和类模板函数的参数不一样。 感觉还是友元函数模板为主导,然后友元函数是啥类型,就决定了使用的类模板类型。
12.11.3.2 举例
manyfrnd.h
#pragma once #includeusing std::cout; using std::endl; template class ManyFriend { private: T item; public: ManyFriend(const T& i) : item(i) {} template friend void show2(C&, D&); }; template void show2(C& c, D& d) { cout << c.item << ", " << d.item << endl; }
main.h
#pragma once #ifndef MAIN_H_ #define MAIN_H_ #include//输入输出 #include "HasFriend.h" //Template_Classes_and_Friends #include "HasFriendT.h" //Template_Classes_and_Friends #include "manyfrnd.h" //Template_Classes_and_Friends using namespace std; void Template_Classes_and_Friends(void) { std::cout << "\nTemplate_Classes_and_Friends Hello************************************************\n"; cout << "Non-template friends*********************************************" << std::endl; 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); cout << "Bound Template Friend Functions***************************************" << std::endl; counts (); HasFriendT hfi3(10); HasFriendT hfi4(20); HasFriendT hfdb1(10.5); report(hfi3); // generate report(HasFriendT &) report(hfi4); // generate report(HasFriendT &) report(hfdb1); // generate report(HasFriendT &) cout << "counts () output:\n"; counts (); cout << "counts () output:\n"; counts (); cout << "Unbound Template Friend Functions*************************************" << std::endl; ManyFriend hfi5(10); ManyFriend hfi6(20); ManyFriend hfdb2(10.5); cout << "hfi5, hfi6: "; show2(hfi5, hfi6); cout << "hfdb2, hfi6: "; show2(hfdb2, hfi6); std::cout << "\nTemplate_Classes_and_Friends Bye************************************************\n"; } #endif
main.cpp
/*
Project name : _12template
Last modified Date: 2022年5月6日11点33分
Last Version: V1.0
Descriptions: 模板类和友元
*/
#include "main.h"
int main()
{
cout << "模板类和友元******************************************************************" << endl;
Template_Classes_and_Friends();
return 0;
}
模板类和友元****************************************************************** Template_Classes_and_Friends Hello************************************************ Non-template friends********************************************* No objects declared: int count: 0; double count: 0 After hfi1 declared: int count: 1; double count: 0 After hfi2 declared: int count: 2; double count: 0 After hfdb declared: int count: 2; double count: 1 HasFriend: 10 HasFriend : 20 HasFriend : 10.5 Bound Template Friend Functions*************************************** template size: 4; template counts(): 0 10 20 10.5 counts () output: template size: 4; template counts(): 2 counts () output: template size: 8; template counts(): 1 Unbound Template Friend Functions************************************* hfi5, hfi6: 10, 20 hfdb2, hfi6: 10.5, 20 Template_Classes_and_Friends Bye************************************************ D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 328)已退出,代码为 0。 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 按任意键关闭此窗口. . .