Overloaded Functions

 

The C language does not support overloaded functions. Consequently, it requires a different functions with  unique identifiers to perform similar but slightly different functionalities.

 

C++ places a high value on polymorphism – i.e., the abilitly to serve the different needs of slightly different situations with the same function.

 

Consider the following example of a string copying routine:

void stringcopy(char* dst, char* src);

void stringcopy(char* dst, char* src, int ct);

 

What’s this? – two function prototypes, each having the same identifier? That’s not legal is it?

 

Notice that except for the fact that one of the two prototypes has an extra calling parameter, the two prototypes are identical. This signifies that the function stringcopy has been overloaded.

 

Overloading supports polymorphism by providing slightly different behavior, depending upon the number of calling parameters or the types of the calling parameters. If any element elements of the prototype signature be different, then the overloading is permitted.

 

 

Ambiguous Overloading:

Ambiguous overloading occurs whenever the compiler cannot determine which of the overloading functions is to be invoked. The ambiguity may be caused by the calling argument not matching any one of the prototypes. For example, the prototypes provide for the first argument being either int or long, but the caller provides a float or double instead. Another ambiguous situation occurs when default arguments are provided in each prototype, but the caller provides no argument at all. The compiler has two different default arguments that it can use, one is int and the other is long. The problem is that the compiler has no way of knowing which of those two default arguments to apply and it therefore cannot choose between the two overloaded functions.

 

 

 

Why Overload Functions?