C++------类模板友元函数类内实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/XUCHEN1230/article/details/86410036
#include <iostream>
#include <string>


//注意头文件:string 和 string.h 的区别;

using namespace std;

//类模板与友元函数---友元函数类内实现:
template<class T1,class T2>
class Person
{
	//友元函数类内实现:
	//默认为全局函数调用属性;
	friend void printPerson(Person<T1,T2>& p)
	{
		cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
	}
public:
	Person(T1 name, T2 age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

private:
	T1 m_Name;
	T2 m_Age;
};
void test01()
{
	Person<string, int>p1("asadasd", 10);
	printPerson(p1);
}

int main()
{
	test01();
	return 0;
}

友元函数

猜你喜欢

转载自blog.csdn.net/XUCHEN1230/article/details/86410036