当前位置: 首页> 科技> IT业 > C++ 70 之 类模版中的成员函数,在类外实现

C++ 70 之 类模版中的成员函数,在类外实现

时间:2025/7/11 23:16:52来源:https://blog.csdn.net/LSG_Down/article/details/139782207 浏览次数:1次
#include <iostream>
#include <string>
using namespace std;template<class T1, class T2>
class Students10{
public:T1 m_name;T2 m_age;Students10(T1 name, T2 age);  // 类内声明   类外实现// {//     this->m_name = name;//     this->m_age = age;// }void show();// {//     cout << "姓名: " << this->m_name << "年龄: " << this->m_age << endl;// }
};// 无论函数中是否用到T1、T2 都要写在类名后面的尖括号中
template<class T1, class T2>
Students10<T1,T2>::Students10(T1 name, T2 age){this->m_name = name;this->m_age = age;
}template<class T1, class T2>
void Students10<T1,T2>::show(){cout << "姓名: " << this->m_name << "年龄: " << this->m_age << endl;
}int main()
{Students10<string, int> stu("张阿三",18);stu.show();return 0;
}

关键字:C++ 70 之 类模版中的成员函数,在类外实现

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: