#include <iostream>
using namespace std;
//类外实现步骤比较麻烦
template <class C1, class C2>
class p;
template <class C1, class C2>
void shows(p <C1, C2>p1) {
cout << "姓名:" << p1.name << endl << "年龄" << p1.age << endl;
}
template <class C1,class C2>
class p {
//全局函数类内实现
friend void show(p <C1,C2>p1) {
cout << "姓名:" << p1.name << endl << "年龄" << p1.age << endl;
}
friend void shows<>(p <C1, C2>p2);
public:
p(C1 name, C2 age) {
this->name = name;
this->age = age;
}
private :
C1 name;
C2 age;
};
int main() {
cout << "全局函数类内实现" << endl;
p<string, int>p1("张三",20);
show(p1);
cout << "全局函数类外实现" << endl;
p<string, int>p2("tom", 20);
shows(p2);
}
C++friend类内类外实现
最新推荐文章于 2024-09-01 16:42:17 发布