c++ 函数模板 类模板

来源:互联网 发布:macbook删除windows 编辑:程序博客网 时间:2024/06/05 09:04

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

函数模板: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];}
0 0
原创粉丝点击