qfedu-cpp-level/day7/d10.cpp

36 lines
604 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 模版类与友元
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class A
{
// 内部实现的友元函数(全局性的友元函数,类的外部可以直接访问 showIn(x) ,不需要类对象调用 a.showIn()
// 接收当前模版类的引用时,可以指定当前模版类的类型
friend void showIn(A<T> &a)
{
cout << a.item << endl;
}
private:
T item;
public:
A(T item)
{
this->item = item;
}
// public:
};
int main()
{
A<int> a(20);
showIn(a);
return 0;
}