Function Name Overloading


In one of the C libraries can be found the following functions:

image from book

 iabs() labs() fabs() dabs() 

image from book

These functions return the absolute value of an int, a long, a float, and a double respectively. You may asked yourself: "Why do we need four functions to accomplish what appears to be the same objective?" To use these functions requires a lot of code. For example (where the "flags" contain values to permit the calling of the correctly function):

image from book

 if (float_flag)    fabs(x);      else        if (long_flag)           labs(x);        else          if (int_flag)             iabs(x);          else             dabs(x); 

image from book

The programmer should not have to work this hard. In C++ one way we can overcome this problem is by using OVERLOADED function names. Function name overloading permits the programmer to use the same function name when the functions have a different signature. This is most useful when the different functions have the same objectives.

Overloading was done when the same operator symbols: +, -, / and * were used on integers, longs, floats or doubles. These are different data types and therefore the function arguments would have to be different, yet the objectives are the same and the operator names are the same. Notice further that overloading was used earlier when the objects cout and cin with the operators << and >> respectively operated regardless of what data type of argument was used. In addition << and >> were overloaded when they used previously with file input and output.

In C++, two or more functions within the same scope may have the same name provided they have different signatures. Two functions have the same signatures, if their signatures have all of the following:

  • the same number of arguments

  • the data types are the same

  • the data types and arguments are in the same order

So to have overloaded functions one of these must be false. The output data type in no way effects the overloading of function names.

For example:

image from book

 int MAX(int a, int b) { return (a>b?a:b); } long MAX(long a, long b) { return (a>b?a:b); } float MAX(float a, float b) { return (a>b?a:b); } double MAX(double a, double b) { return (a>b?a:b); } 

image from book

are functions that overload the name MAX. See OVERLOAD.CPP The C++ compiler will not return an error when the definitions for the functions MAX are compiled since the signatures of these functions are all different.

 Note:  Earlier C++ compilers required the word "overloaded" to precede the definition of overloaded functions. However, this is no longer necessary in Standard C++.

Overloading may help the programmer who wants a function to operate on a new argument. Rather than rewriting all previous code to include this new argument, the programmer only needs to overload the function name with a new definition. The compiler will then determine from context which name to use. What overloading has done is to reduce the complexity of programming by reducing the number of different function names for the programmer to remember.

Q: When should functions be overloaded?

A: When there exists a group of functions with similar results but different signatures. However, the real answer to this question will only come with experience. There will be times in which another technique will be a better choice.

Q: How does a compiler overload a function name?

A: In reality the compiler does not use the same name for each function. It performs a process called NAME MANGLING. Each compiler uses its own specific method to change the name. In general the programmer does not need to be concerned about this process. However, there are cases where care must be taken. For this you need to consult the compiler documentation.

Name mangling can cause problems especially when dealing with C libraries or when moving function names across file boundaries. Should it be necessary to work in Windows with DLL (dynamic linked library) files, name mangling may cause trouble. Name mangling causes trouble because the name in the program used by the compiler no longer agrees with its definition's name.

See OVERLOD2.CPP and OVERLOD3.CPP




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net