1.指定传入的类型 直接显示对象的数据类型
2.参数模板化 将对象中的参数变为模板进行传递
3.整个类模板化 将这个对象模型 模板化进行传递
#include <iostream>
#include <string>
using namespace std;
//类模板对象做函数参数
//1.指定传入的类型 直接显示对象的数据类型
//2.参数模板化 将对象中的参数变为模板进行传递
//3.整个类模板化 将这个对象模型 模板化进行传递
template <class T1,class T2>
class Person
{
public:
Person(T1 name,T2 age)
{
this->m_Age = age;
this->m_Name = name;
}
void showInfo()
{
cout << "name : " << this->m_Name << endl;
cout << "age : " << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
//1.指定传入的类型 直接显示对象的数据类型-最常使用
void showPerson1(Person <string, int>&p)
{
p.showInfo();
}
void test100()
{
Person <string, int>p1("孙悟空", 5000);
showPerson1(p1);
}
//2.参数模板化 将对象中的参数变为模板进行传递-相当于类模板配合着函数模板使用
template <class T1,class T2>
void showPerson2(Person<T1, T2>& p)
{
p.showInfo();
//查看一个类型的信息
cout << "T1的类型为: " << typeid(T1).name() << endl;
cout << "T2的类型为: " << typeid(T2).name() << endl;
}
void test200()
{
Person <string, int>p2("唐僧", 20);
showPerson2(p2);
}
//3.整个类模板化 将这个对象模型 模板化进行传递-相当于类模板配合着函数模板使用
template <class T>
void showPerson3(T &p)
{
p.showInfo();
cout << "showPerson3推导的T的类型为: " << typeid(T).name() << endl;
}
void test300()
{
Person <string, int>p3("牛魔王", 5000);
showPerson3(p3);
}
int main4()
{
test100();
test200();
test300();
system("pause");
return 0;
}