October 1997
Intermediate to advanced
800 pages
20h 48m
English
Let's examine the following function, which determines a maximum from its two integer arguments.
inline int max(int a, int b) {
return a > b ? a : b;
}
int m = 43, n = 56;
cout << max(m, n) << endl; // displays 56 (CORRECT)
double x = 4.3, y = 5.6;
cout << max(x, y) << endl; // displays 5 (WRONG)
The first call to max() works, but the second one does not. Calls to max() with integer arguments match its signature exactly, but double arguments truncate fractional parts because of conversion rules (your compiler may report warnings here). The first cout statement displays the correct maximum (56), but the second cout statement does not.
We could overload max() with double arguments, but the code to determine ...