#include<iostream>
using namespace std;
template<class T1,class T2>
class Person {
public:
Person(T1 name, T2 age);
/*Person(T1 name, T2 age) {
this->m_Name = name;
this->m_Age = age;
}*/
void showPerson();
/*void showPerson() {
cout << "姓名: " << this->m_Name << endl;
cout << "年龄: " << this->m_Age << endl;
}*/
T1 m_Name;
T2 m_Age;
};
//模板-构造函数类外实现
template<class T1,class T2>
//错误代码:E0135:类模板person<T1,T2>没有成员Person
//错误原因:未在类内做出函数声明
Person<T1, T2>::Person(T1 name, T2 age) {
this->m_Name = name;
this->m_Age = age;
}
//模板-成员函数类外实现
//即使该成员函数未用到T,也必须加上模板相关
template<class T1,class T2>
void Person<T1, T2>::showPerson() {
cout << "姓名: " << this->m_Name << endl << "姓名: " << this->m_Age << endl;
}
void test01() {
Person<string, int>P("周公瑾", 20);
P.showPerson();
}
int main() {
test01();
}
c++笔记 模板 类模板成员函数类外实现
最新推荐文章于 2023-06-24 17:58:06 发布