第五章 Template.docVIP

  • 5
  • 0
  • 约5.34千字
  • 约 6页
  • 2017-06-10 发布于北京
  • 举报
Templates 定义一个模板使用的关键字:class和typename。 在定义作为模板的时候:class和typename,作用是一样的。 一 函数模板 1 定义方式 template class identifier function_declaration; template typename identifier function_declaration; 2 Example template class T T GetMax (T a, T b) { T result; result = (ab)? a : b; return (result); } 3 使用方法 function_name type (parameters); int x,y; GetMax int (x,y); 对于内部类型,通常是可以无需制定具体的类型,编译器会自动识别, 而是写成: int x,y; GetMax(x,y); //x,y为相同的类型 int x; long y; GetMax(x,y); //x,y为不相同的类型 报错 两种不同的内部类型 必须: template class T, class U T GetMin (T a, U b) { return (ab?a:b); } int i,j; long l; i = GetMinint,long (j,

文档评论(0)

1亿VIP精品文档

相关文档