模板 Templates 模板函数 模板类

来源:互联网 发布:小米5s plus 知乎 编辑:程序博客网 时间:2024/04/29 15:41

模板函数

template <class identifier> function_declaration;

template <typename identifier> function_declaration;


template <class myType>myType GetMax (myType a, myType b) { return (a>b?a:b);}


int x,y;GetMax <int> (x,y);

编译器自动补全类型(因为作参数的变量有类型)

GetMax (i,j);

模板类

template <class T>class mypair {    T values [2];  public:    mypair (T first, T second)    {      values[0]=first; values[1]=second;    }};

模板需要在头文件,因为实现还没有给出。



原创粉丝点击