In depth analysis of C++ function overloading

  • 2020-04-02 01:23:09
  • OfStack

When we open bottles and cans, we often suffer from the embarrassment of not being able to find the right tool because of the different sizes of the bottles. So sometimes just to open a bottle, home to a variety of specifications of the bottle opener. Why bother opening a bottle? So someone invented a multifunctional bottle opener that could be opened easily from a bottle of beer to a bottle of red wine with a cork.

However, problems with bottle openers can also occur in programming. For example, if we want to write a function to find the absolute value of a number, integer, floating point, and double Numbers all have absolute values, but the return value type of the function written for them is different. Such as:


int iabs(int a);
float fabs(float a);
double dabs(double a);

Isn't it a bit like having multiple bottle openers? Can we also make a multi-functional bottle opener in programming, and give the absolute value of all data types to a function called abs?

In C++, we are also able to consolidate functions with the same function into one function without having to write many functions with different names. This is called the function's chong load. The essence of overloading is that multiple functions share the same function name.

Let's start with an example of function overloading :(program 6.3)


#include "iostream.h"
int abs(int a);//Function prototype when the parameter is integer data
float abs(float a);//Function prototype when the argument is floating point data
double abs(double a);//Function prototype when the parameter is double precision data
int main()
{
   int a=-5,b=3;
   float c=-2.4f,d=8.4f;
   double e=-3e-9,f=3e6;
   cout <<"a=" <<abs(a) <<endl <<"b=" <<abs(b) <<endl;//The output function returns the result
   cout <<"c=" <<abs(c) <<endl <<"d=" <<abs(d) <<endl;
   cout <<"e=" <<abs(e) <<endl <<"f=" <<abs(f) <<endl;
   return 0;
}
int abs(int a)//The function definitions
{
   cout <<"int abs" <<endl;//Shows which function is running
   return (a>=0?a:-a);//If a is greater than or equal to zero it returns a, otherwise it returns minus a.
}
float abs(float a)
{
   cout <<"float abs" <<endl;
   return (a>=0?a:-a);
}
double abs(double a)
{
   cout <<"double abs" <<endl;
   return (a>=0?a:-a);
}
 Operation results: 
int abs
int abs
a=5
b=3
float abs
float abs
c=2.4
d=8.4
double abs
double abs
e=3e-009
f=3e+006

The results show that the abs function can handle three different data types. So how can we build our own "multi-tool"?

It's not too much trouble to write an overloaded function. First, we need to tell the computer that there are multiple definitions for the same function name, so we need to write multiple function prototypes for the same function name (like lines 2 to 4 in 6.3). Secondly, we need to correspond to these function prototypes and write the definitions of these functions (such as the definition of the three abs functions after the main function body of program 6.3).

But how do computers identify the "tools" used in different environments?
If we don't know which tool to use, we will put each tool on it and try it out. If there is only one tool that is suitable, then we can be sure of using it without any doubt. But if there are two or more tools that work, we don't know which is the right one to use.

The way computers do it is similar to ours. The computer determines which function to run based on the number of arguments in the argument table, the data type and order of each argument when the function is declared. Therefore, when the overloaded function parameter list is identical, the computer cannot decide which function to run, and the program fails.

After learning how computers recognize overloaded functions, there are a few things to note about writing an overloaded function: in an overloaded function, the number of arguments, the data types, and the order of each argument in the argument table of any two functions cannot be exactly the same. For example, int func(int a,char b) and float func(int c,char d) cannot be overridden because they have exactly the same number of arguments, type and order, even if they have different parameter names and return value types.

When an overloaded function is called, it may occur that a completely inappropriate function is not found. At this point, a data type conversion is required. Because this approach can lead to data loss or inconsistent data types, which can be avoided if the problem is fully considered, it is not discussed here. Interested readers can refer to other C++ resources.

Algorithm time: overloaded function
In a sense, an overloaded function is convenient for the user of the function. As we learned in the previous section, if you've written all the functions, then building a program is as easy as building blocks. However, if there are too many functions with similar functions but different names, it may not be easy to build many "building blocks". When the function writer thinks enough about the slightly different functions that should be run in different situations, the function user doesn't have to worry about the little details. However, the function name of the overloaded function should still conform to its function, if the function of completely different function overload, then greatly affect the readability of the program.


Related articles: