In-depth analysis of C ++ function Overloading

Source: Internet
Author: User

In C ++, we can also integrate functions with the same function into a function without having to write many functions with different function names. This is called function overloading. The following is a detailed analysis of function overloading in C ++. For more information, see

When we open a bottle or tank, we often encounter the embarrassment that we cannot find the proper tool because of the different bottle port specifications. So sometimes, in order to open a bottle, a variety of bottle openers should be prepared at home. Why is it so troublesome to open a bottle? So someone invented the multi-function bottle opener, which can be easily opened no matter whether it is a beer bottle or a cork wine bottle.

However, problems with the bottle opener may also occur in programming. For example, we need to write a function to calculate the absolute value of a number. However, integers, floating-point numbers, and double-precision numbers all have absolute values, but the return values of the functions written for them are different. For example:

Copy codeThe Code is as follows:
Int iabs (int );
Float fabs (float );
Double dabs (double );


Is it a bit of a feeling of a variety of bottle openers? Can we also create a multi-function bottle opener in the program design to give the absolute values of all data types to the abs function?

In C ++, we can also integrate functions with the same function into a function without having to write many functions with different function names, this is called the Overload of the function ). In essence, multiple functions share the same function name.

Let's first look at an example of function overload: (Program 6.3)

Copy codeThe Code is as follows:
# Include "iostream. h"
Int abs (int a); // The function prototype when the parameter is an integer.
Float abs (float a); // function prototype when the parameter 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; // output the result returned by the function
Cout <"c =" <abs (c) <endl <"d =" <abs (d) <endl;
Cout <"e =" <abs (e) <endl <"f =" <abs (f) <endl;
Return 0;
}
Int abs (int a) // Function Definition
{
Cout <"int abs" <endl; // displays the function that is running.
Return (a> = 0? A:-a); // if a is greater than or equal to zero, return a; otherwise, return-.
}
Float abs (float)
{
Cout <"float abs" <endl;
Return (a> = 0? A:-);
}
Double abs (double)
{
Cout <"double abs" <endl;
Return (a> = 0? A:-);
}
Running result:
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 running results show that the abs function can process three different data types. How can we create a "Multi-Function Tool" by ourselves?

In fact, it is not very troublesome to compile an overloaded function. First, we need to tell the computer that there are multiple definitions for the same function name. Therefore, we need to write multiple function prototypes for the same function name (such as the second to fourth rows of the program 6.3 ); next, we need to correspond to these function prototypes and write the definitions of these functions respectively (for example, the definition of three abs functions after the main function body of program 6.3 ).

However, how does a computer identify these "tools" used in different environments?
Multi-functional tools are used in daily life. If we don't know which tool should be used, we will give each tool a try. If only one tool is suitable, then we can be sure to use it. However, if two or more tools are suitable, we cannot tell which one should be used correctly.

Computer practices are similar to ours. The computer determines which function to run based on the number of parameters in the parameter table, the Data Type and sequence of each parameter during function declaration. Therefore, when the parameters of the overload function are identical, the computer cannot determine which function should be run, so the program will go wrong.

After learning how the computer recognizes heavy-duty functions, we find that you still need to pay attention to writing a heavy-duty function, that is, in the heavy-load function, the number of parameters in the parameter table of any two functions, the Data Type and sequence of each parameter cannot be exactly the same. For example, int func (int a, char B) and float func (int c, char d) cannot be overloaded, because they have the same number of parameters, the types and sequence of parameters, even if the parameter names are different and the return value type is different, it is useless.

When calling an overloaded function, a fully appropriate function may not be found. At this time, you need to convert the data type. This method may cause data loss or data types are not strictly matched, and this situation can be avoided as much as possible after full consideration. Therefore, we will not discuss this issue here. Interested readers can refer to other C ++ references.

Algorithm time: overload Function
In a sense, overload functions are convenient for function users. In the previous section, we know that if all functions are compiled, it is as easy as building blocks to complete a program. However, if function names are similar but there are too many different functions, it may not be easy to build multiple "blocks. When function writers fully consider how to run slightly different functions under different circumstances, function users do not have to worry about these small details. However, the function name of the overload function should still conform to its function. If function names with different functions are reloaded, the readability of the program is greatly affected.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.