首页 > 学院 > 开发设计 > 正文

template类

2019-11-11 05:06:06
字体:
来源:转载
供稿:网友
在工程中应将声明和定义分开并置于头文件中#include<iostream>using namespace std;template<typename T>class Operator{public: T add(T a,T b);       //要将声明和定义分开  T minus(T a,T b);}; template<typename T>        //必须加template 声明T Operator<T>::add(T a,T b)               //<T>不可少{ return a+b;}template<typename T>T Operator<T>::minus(T a,T b){ return a-b;}int main(){ Operator<int> op1; cout<<op1.add(5,6)<<endl;  Operator<float> op2; cout<<op2.minus(3.14,1.22)<<endl; return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表