能够掌握类模板中的成员函数的类外实现:
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
template<typename T1, typename T2>
class Person
{
public:
T1 m_Name;
T2 m_Age;
Person(T1 name, T2 age);
void showPerson();
};
// 构造函数类外实现
template<typename T1, typename T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
m_Name = name;
m_Age = age;
}
// 成员函数的类外实现
template<typename T1, typename T2>
void Person<T1, T2>::showPerson()
{
cout << "姓名:" << m_Name << " 年龄:" << m_Age << endl;
}
void test()
{
Person<string, int> p("张三", 18);
p.showPerson();
}
int main(void)
{
test();
system("pause");
return 0;
}
运行截图: