友元内部实现
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<numeric>
template <class NameType,class AgeType>
class Mark {
friend void printMaker(Mark<NameType, AgeType>& p) {
std::cout << "class inside create\n" << p.name << ' ' << p.age << '\n';
}
public:
Mark(NameType name, AgeType age);
//void printMark();
private:
NameType name;
AgeType age;
};
template<class NameType,class AgeType>
Mark<NameType, AgeType>::Mark(NameType name, AgeType age) {
this->name = name;
this->age = age;
}
void test01() {
Mark<std::string, int> m("123123", 200);
printMaker(m);
}
int main() {
test01();
return 0;
}
友元外部实现
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<numeric>
//template <class NameType,class AgeType>
//class Mark;
//
//template <class NameType,class AgeType>
//void printMaker(Mark<NameType, AgeType>& p);
template <class NameType,class AgeType>
class Mark {
template<class NameType, class AgeType>
friend void printMaker(Mark<NameType, AgeType>& p);
public:
Mark(NameType name, AgeType age);
//void printMark();
private:
NameType name;
AgeType age;
};
template<class NameType,class AgeType>
Mark<NameType, AgeType>::Mark(NameType name, AgeType age) {
this->name = name;
this->age = age;
}
template<class NameType,class AgeType>
void printMaker(Mark<NameType,AgeType>&p) {
std::cout << p.name << ' ' << p.age << '\n';
}
void test01() {
Mark<std::string, int> m("123123", 200);
printMaker(m);
}
int main() {
test01();
return 0;
}
标签:友元,name,age,Mark,template,include,printMaker,模板
From: https://www.cnblogs.com/lambdaios/p/17962744