Topics o 14.1 Introduction o 14.2 Function Templates o 14.3 Overloading Function Templates o 14.4 Class Templates o 14.5 Nontype Parameters and Default Types for Class Templates o 14.6 Notes on Templates and Friends o 14.7 Notes on Templates and static Members 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 Topics 14.1 Introduction 14.2 Function Templates 14.3 Overloading Function Templates 14.4 Class Templates 14.5 Nontype Parameters and Default Types for Class Templates 14.6 Notes on Templates and Friends 14.7 Notes on Templates and static Members
14.2 Function Templates review °定义模板: template关键字+尖括号<>括起来的 template parameter list(模板参数列表) °模板参数列表: 参数称为 I formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词 typename(或cass)起 头,参数和参数之间必须以逗号分隔,如 template typename T, typename V> 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 14.2 Function Templates -- review 定义模板: template关键字+尖括号<>括起来的template parameter list(模板参数列表) 模板参数列表: 参数称为formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词typename (或class )起 头,参数和参数之间必须以逗号分隔,如 template < typename T, typename V>
14.2 Function Templates revlew template typename T>//or template< class> T maximum(t value 1, T value 2, T value3) T maximumValue= value 1: / assume value l is maximum n/ determine whether value2 is greater than maximumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 return maximumvalue )//end function template maximum 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 14.2 Function Templates -- review template < typename T > // or template< classT > T maximum( T value1, T value2, T value3 ) { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum
14.2 Function Templates revlew template< typename T>∥ or template< class>∥模板声明 T maximun( T value1, T value2, T value3)∥/函数模板定义 T maximumValue= value 1: / assume value l is maximum T指定函数返回值、形参以及局部变量的类型 determIne Whether value2 Is greater than maxImumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 函数调用形式: return maximumvalue maximum(1, 2, 3); )//end function template maximum maximum(intl, int2, int3); 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 14.2 Function Templates -- review template < typename T > // or template< classT > //模板声明 T maximum( T value1, T value2, T value3 ) //函数模板定义 { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum T指定函数返回值、形参以及局部变量的类型 函数调用形式: maximum (1, 2, 3); maximum (int1, int2, int3);
14.2 Function Templates 定义 o template< typename T> o template< class ElementType r o template< typename BorderType, typename FileType cas; typename(关键字):.函数模板类型参数,任何内 部类型或用户自定义类型。 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 14.2 Function Templates -- 定义 template< typename T > or template< class ElementType > or template< typename BorderType, typename FillType > class,typename(关键字):函数模板类型参数,任何内 部类型或用户自定义类型