qfedu-cpp-level/day7/d12.cpp

42 lines
606 B
C++
Raw Permalink Normal View History

2023-08-01 18:51:53 +08:00
// 友元函数模板
/*
:
template <typename T>
friend (T &);
*/
#include <iostream>
using namespace std;
template <typename T>
class A
{
// 友元函数模板,声明的友元函数名不需要加<>空泛型
template <typename U>
friend void show(A<U> &a);
private:
T item;
public:
A(T item)
{
this->item = item;
}
};
template <typename U>
void show(A<U> &a)
{
cout << "template friend item is: " << a.item << endl;
}
int main()
{
A<int> a(15);
show(a);
A<float> b(15.5);
show(b);
return 0;
}