程序员社区

c++模板学习08之类模板对象做函数参数

#include<iostream>
#include<string>
using namespace std;
//类模板对象做函数参数
template<class T1,class T2>
class Person {
public:
	Person(T1 name, T2 age) :name(name),age(age){}
	T1 name;
	T2 age;
	void showPerson() 
	{
		cout << "姓名: " << name << " 年龄:" << age << endl;
	}
};
//1.指定传入类型(把类模板创建的对象p传入函数showPerosn)
void showPerson(Person<string,int>&p)
{
	p.showPerson();
}
//2.参数模板化
template<class T1,class T2>
void showPerson2(Person<T1,T2>&p)
{
	p.showPerson();
	cout << "T1的数据类型为:" << typeid(T1).name() << endl;
	cout << "T1的数据类型为:" << typeid(T2).name() << endl;
}
//3.整个类模板化
template<class T>
void showPerson3(T &p)
{
	p.showPerson();
	cout << "T的数据类型为" << typeid(T).name() << endl;
}
void test()
{
	Person<string, int> p("大忽悠", 18);
	showPerson3(p);
}

int main()
{
	test();
	system("pause");
	return 0;
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » c++模板学习08之类模板对象做函数参数

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区