#include<iostream>
using namespace std;
//类模板成员函数类外实现
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age);
/*{
this->m_Name=name;
this->m_Age = age;
}*/
void showPerson();
//{
// cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
//}
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 test01()
{
Person<string, int>p("Tom", 20);
}
int main()
{
test01();
system("pause");
return 0;
}
1.3.6 类模板成员函数类外实现
最新推荐文章于 2024-07-26 18:05:18 发布
这篇博客展示了如何在C++中实现类模板的成员函数,并在类外定义构造函数和成员方法。通过一个`Person`模板类的例子,详细解释了如何使用字符串和整数类型作为模板参数,并在`main`函数中创建了一个实例进行测试。
256

被折叠的 条评论
为什么被折叠?



