C++类模板成员函数类外实现

学习目标:能够掌握类模板中成员函数类外实现

代码示例:

#include<iostream>
#include<string>
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();
       return 0;
}

总结:类模板中成员函数类外实现时,需要加上模板参数列表

C/C++基础 文章被收录于专栏

本专栏收录C/C++编程语言相关知识

全部评论

相关推荐

码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务