类模板构造函数和成员函数的类外实现示例:
#include<iostream>
using namespace std;
#include<string>
template<class T1,class T2>
class Person
{
public:
Person(string name, int age);//构造函数类内声明
//{
// this->m_Name = name;
// this->m_Age = age;
//}
void showPerson();//成员函数类内声明
//{
//
//}
T1 m_Name;
T2 m_Age;
};
template<class T1,class T2>
Person<T1 ,T2>::Person(string name, int 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 test01()
{
Person<string, int> p("Tom", 23);
p.showPerson();
}
int main()
{
test01();
return 0;
}