8.4 Function Overloading
) Function overloading, also called function polymorphism, lets you use multiple functions with the same name, but using different argument lists. The key to function overloading is a function's argument list, also called the function signature. The signature can differ in the number of arguments or in the type of arguments, or both.
) When there is one type mismatch between function calling and function prototype, C++ will accept this function call and conduct type conversion:
void process(double x, int y); process(2,3); // acceptable, converting 2 to double(2.0)
But if the function call doen't match any of the prototypes while still could be converted to more than one prototype, C++ rejects this ambiguous function call:
void process(double x, int y); void process(float x, int y); process(2, 6); // invalid! 2 could be converted to both double and float
) This is not overloading:
void process(double x); void process(double & x); // not overloading
due to the fact that the compiler considers a reference to a type and a type itself to be the same signature(to avoid ambiguous situations).
) The function-matching process also discriminates between const and non-const:
void process(double x); void process(const double x); // overloaded
) Difference in the type of return value doesn't enable function overloading:
long process(double x); double process(double x); // invalid!
Here's an example:
1 // leftover.cpp -- overloading the left() function 2 #include <iostream> 3 unsigned long left(unsigned long num, unsigned ct); 4 char * left(const char * str, int n = 1); 5 6 int main() 7 { 8 using namespace std; 9 char * trip = "Hawaii!!"; 10 unsigned long n = 12345678; 11 int i; 12 char * temp; 13 14 for (i = 1; i < 10; i++) 15 { 16 cout << left(n,i) << endl; 17 temp = left(trip,i); 18 cout << temp << endl; 19 delete [] temp; 20 } 21 return 0; 22 } 23 24 // This function returns the first ct digits of the number num 25 unsigned long left(unsigned long num, unsigned ct) 26 { 27 unsigned digits = 1; 28 unsigned long n = num; 29 30 if (ct == 0 || num == 0) 31 return 0; 32 while (n/=10) 33 digits++; 34 if (digits > ct) 35 { 36 ct = digits - ct; 37 while (ct--) 38 num /= 10; 39 return num; 40 } 41 else 42 return num; 43 } 44 45 // This function returns a pointer to a new string consisting of the first n characters in the old string 46 char * left(const char * str, int n) 47 { 48 if (n < 0) 49 n = 0; 50 char * p = new char[n+1]; 51 int i; 52 for (int i = 0; i < n && str[i]; i++) 53 p[i] = str[i]; 54 while (i <= n) 55 p[i++] = '