首页 > 编程 > C++ > 正文

c++ 函数模板 类模板

2019-11-06 07:34:43
字体:
来源:转载
供稿:网友

函数模板与类模板的定义方式

函数模板:template <模板参数表>返回类型 函数名(形参表){ //函数体}例:template <typename T>T add(T t1,T t2){ return t1+t2;}类模板:template <模板参数表>class 类名{ 成员名};在外部定义类模板的成员函数template <模板参数表>返回类型 类名<模板参数表>::函数名(参数表){ //函数体}例:template<typename Tno,typename Tscore,int num>class Student{ PRivate: Tno studentId[num]; Tscore studentScore[num]; public: Tno TopStudent(); };template <typename Tno,typename Tscore,int num>Tno Student<Tno,Tscore,num>::TopStudent(){ return studenId[0];}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选