首页 > 编程语言 > 详细

c++ template

时间:2019-02-11 18:31:32      阅读:212      评论:0      收藏:0      [点我收藏+]

1 c++ template示例

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

2 定义类和类外函数的时候,都是直接在前面加上template <class T>即可,并且定义类外函数的时候要加上<T>的限定

 

c++ template

原文:https://www.cnblogs.com/hustdc/p/10362539.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!