/*
* 类模板成员函数类外实现
* 类模板成员函数类外实现时,需要加上模板参数列表
*/
#include<iostream>
#include<string>
using namespace std;
//类模板中成员函数类外实现
template<class T1,class T2>
class Person
{
public:
//成员函数类内声明
Person(T1 name, T2 age);
void showPerson();
public:
T1 m_Name;
T2 m_Age;
};
//构造函数 类外实现
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
//成员函数 类外实现
template<class T1,class T2>
void Person<T1, T2>::showPerson()
{
cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
}
void test03()
{
Person<string, int>p("Tom", 20);
p.showPerson();
}
int main()
{
test03();
cout<<"Hello World!"<<endl;
system("pause");
return 0;
}
07-01
2662
02-17
4135
04-08
4353
07-17
2万+
09-22
601