OOPortal
J2EEOnline RationalDB
prev next prev next
  Course navigation
 
Lesson 7
Objective
Overloading functions
Compiler picks the appropriate version of an overloaded function.
   
Overloaded functions are an important addition in C++. The overloaded meaning is selected by matching the argument list of the function call to the argument list of the function declaration. The function argument list is called the signature. The return type is not a part of the signature, but the order of the arguments is crucial.
Consider the following overloaded function declarations:
void print(int i = 0);   //signature is int
void print(int i, double x);   //int, double
void print(double y, int i);   //double, int
  1. a best match
  2. an ambiguous match
  3. no match

Without a best match, the compiler issues an appropriate syntax error:
print('A');   //converts and matches int
print(str[]);     //no match, wrong type
print(15, 9);     //ambiguous
print(15, 9.0);   //matches int, double
print();          //matches int by default
  Course navigation