正在加载图片...
14.2 Function Templates 函数处理过程 ①在程序中说明了一个函数模板 ②编译系统发现有一个相应的函数调用 例如: printArray(a, ACOUNT) ③编译器寻找和使用最符合函数名和参数类型的函数调用 ④根据实参中的类型来确认是否匹配函数模板中对应的形参, 然后生成一个模板函数(函数模板特化)。(该模板函数的 定义体与函数模板的函数定义体相同) 例如:对于int类型的模板特化: void printArray const d for( inti=OiI< co, void printArray( const int*, int ) cout<< arrayli1< void printArray( const double*, int ) cout<<end: )void printArray( const char*, int ) ⑤编译这个新建的函数 0 2018, SEU. All rights reserved. 14© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 14 14.2 Function Templates -- 函数处理过程 ① 在程序中说明了一个函数模板 ② 编译系统发现有一个相应的函数调用 例如:printArray( a, ACOUNT ); ③ 编译器寻找和使用最符合函数名和参数类型的函数调用 ④ 根据实参中的类型来确认是否匹配函数模板中对应的形参, 然后生成一个模板函数(函数模板特化)。(该模板函数的 定义体与函数模板的函数定义体相同) 例如:对于int类型的模板特化: void printArray( const int *array, int count ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; } ⑤ 编译这个新建的函数 void printArray( const int *, int ); void printArray( const double *, int ); void printArray( const char *, int );
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有