5.1 三种方式
5.1.1 显示指定类型
5.1.2 参数模板化
5.1.3 整体模板化
5.2 查看类型名称
5.2.1 cout << typeid(T).name() << endl;
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
template<class T1,class T2>
class Modulator {
public:
Modulator(T1 name, T2 age) {
this->m_age = age;
this->m_name = name;
}
void show() {
cout << "名字:" << this->m_name << " 年龄:" << this->m_age << endl;
}
T1 m_name;
T2 m_age;
};
//1 指定传入类型
void doBusiness(Modulator<string, int> &modul) {
modul.show();
}
void test01() {
Modulator<string, int> mod("渣渣猫", 26);
doBusiness(mod);
}
//2 参数模板化
template<class T1,class T2>
void doBusiness2(Modulator<T1, T2> &modul) {
//如何查看类型
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
modul.show();
}
void test02() {
Modulator<string, int> mod("土拨鼠", 25);
doBusiness2(mod);
}
//3 整体模板化
template<class T>
void doBusiness3(T &p) {
cout << typeid(T).name() << endl;
p.show();
}
void test03() {
Modulator<string, int> mod("锯嘴葫芦", 19);
doBusiness3(mod);
}
int main(){
test01();
test02();
test03();
system("pause");
return 0;
}