//类模板对象做函数参数

#include
using namespace std;

//类模板对象做函数参数
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Age = age;
this->m_Name = name;
}

void showPerosn()
{
	cout << "姓名: " << this->m_Name << "年龄:" << this->m_Age << endl;
}

T1 m_Name;
T2 m_Age;

};
//1.指定传入类型
void printPerson1(Person<string, int>&p)
{
p.showPerosn();
}

void test01()
{
Person<string, int>p(“孙悟空”, 100);
printPerson1§;
}

//2.参数模板化
template<class T1,class T2>
void printPerson2(Person<T1, T2>&p)
{
p.showPerosn();
//查看他自动推出来的时什么类型下面
cout << “T1的类型为:” << typeid(T1).name() << endl;
cout << “T2的类型为:” << typeid(T2).name() << endl;
}
void test02()
{
Person<string, int>p(“猪八戒”, 90);
printPerson2§;
}

int main()
{

test01();
test02();

system("pause");
return 0;

}

猜你喜欢

转载自blog.csdn.net/ADADQDQQ/article/details/108434432