template question
Write your question here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
template <class T>
T Max(T a, T b)
{
cout <<"template:";
return a < b ? b: a;
}
#include<iostream.h>
int Max(int x, int y)
{
cout <<"Loading function0:";
return (x > y) ? x : y;
}
int Max(char x,int y)
{
cout<<"Loading function1:";
return (x > y) ? x : y;
}
int Max(int x, char y)
{
cout<<"Loading function2:";
return (x > y) ? x : y;
}
void main()
{
int i= 10;
char c='a';
double d=2.14;
cout<<Max(d,i)<<endl;
cout<<Max(i,d)<<endl;
}
|
result:
Loading function1:10
Loading function2:10
Press any key to continue
why not call "int Max(int x, int y)" ?
It looks like you are using an old compiler. It should give an error.
error: call of overloaded ‘Max(double&, int&)’ is ambiguous
note: candidates are: int Max(int, int)
note: int Max(char, int)
note: int Max(int, char) |
Topic archived. No new replies allowed.