#include<iostream>
#include<string>
using namespace std;
template<class T1,class T2>
class Car;
//全局函数不用作用域
template<class T1,class T2>
void printCar2(Car<T1,T2> p)
{
cout << "类外实现:name = " << p.c_name << " age = " << p.c_age << endl;
}
template<class T1,class T2>
class Car
{
//全局函数类内实现
friend void printCar(Car<T1,T2> p)
{
cout << "类内实现:name = " << p.c_name << " age = " << p.c_age << endl;
}
// 全局函数类外实现 :
// 加一个空模板的参数列表<>
// 如果全局函数类外实现,需要让编译器提前知道这个函数存在
friend void printCar2<>(Car<T1,T2> p);
public:
Car(T1 name,T2 age)
{
this->c_age = age;
this->c_name = name;
}
T1 c_name;
T2 c_age;
};
void test_16()
{
Car<string,int> c("tom",3);
printCar(c);
printCar2(c);
}
int main()
{
test_16();
}
26-【其实学C最怕的就是拖着,越演到中场戏越肝不动了】类模板 X 友元
猜你喜欢
转载自blog.csdn.net/magic_shuang/article/details/107592005
今日推荐
周排行