C++编程(B)

Educn 2 outh templateclass t void outconst t t

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: template<class T> T max(T,T); const int s = 7; void k(){ max(1,2); // max<int> max('a','b'); // max<char> max(2.7,4.9); // max<double> max(s,7); // max<int>(int(s),7) max(‘a’,1); // 错误,歧义 max(2.7,4); // 错误,歧义 } void f() { // 显式限定,避免歧义 max<int>(‘a’,1); max<double>(2.7,4); } Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 模板函数重载示例 也可以增加适当的声明来避免歧义 int max(int i, int j) {return max<int>(i,j);} double max(int i, double d){return max<double>(i,d);} double max(double d, int i){return max<double>(d,i);} double max(double d1, double d2) {return max<double>(d1,d2);} void g() { max('a',1); // max(in...
View Full Document

Ask a homework question - tutors are online