| Lesson 7 || Overloading functions |
| Objective || Compiler picks the appropriate version of an overloaded function. |
Overloading Functions in C++
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
When the print() function is invoked, the compiler matches the actual arguments to the various signatures and picks the best match.
This is called the signature-matching algorithm.
In general, there are three possibilities:
- a best match
- an ambiguous match
- 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