What Is Function Overloading?
Overloaded functions are a special case of functions. For convenience, C ++ allows several functions of the same name to be declared in the same scope, but the formal parameters (referring to the number, type, or order of the parameters) of these functions must be different , That is to say use the same function to complete different functions. This is the overloaded function. Overloaded functions are often used to implement problems with similar functions but different data types. It's not just function return value types that are different.
Overloaded function
- The two overloaded functions must differ in one or both of the following ways:
- 1,
- Overloaded function is a special function supported by C ++. C ++ compiler's judgment of function overloading is one of the most complicated contents in C ++ language.
- First of all, let's clarify the definition of overloaded functions: the function names in the same declaration domain are the same, but the parameter tables are different, that is, a special function that uniquely identifies and distinguishes a function by its parameter table.
- You may be asking, why are functions overloaded? When should you choose function overloading, and when shouldn't you? This is what I will introduce below.
- Function overloading is actually the idea of "one thing and multiple uses" (here, "thing" refers to "function name"). In fact, not only functions can be overloaded, but operators can also be overloaded. For example, the operators "<<" and ">>" can be used both as shift operators and as insertion operators in the output stream and extraction operators in the input stream.
- When you are defining a set of functions that cause them to perform a series of operations, they are applied to different parameter types. At this point we can choose to overload the function.
- For example: int z_x_max (int, int); // returns the maximum value of two integers;
- int ve_max (const vector <int> &); // Returns the maximum value in the vector container;
- int matrix_max (const matrix &); // Returns the maximum value of matrix reference;
- The above three functions can be roughly described as judging the maximum value in a set of numbers. For the users of the function, they do not care about the details of the function definition, that is, they do not care about judging the size and judgment of two integers. The size of the array (vector container) number should use different functions, but for the designer of the program, this has to be thought of. The programmer must remember and look up each function name. Function overloading frees programmers from the complexity of this problem, and C ++ provides this support. The three comparison functions above can be defined as:
- int Max (int, int); // Returns the maximum value of two integers;
- int Max (const vector <int> &); // Returns the maximum value in the vector container;
- int Max (const matrix &); // Returns the maximum value of matrix reference;
- Parameters can be used to distinguish different functions at a glance.
- At the same time, the overloading of the function may not be applicable. For example: In the process of developing a text editor, a series of functions for controlling the cursor are involved, as follows:
- Screen & MoveUp ();
- Screen & MoveDown ();
- Screen & MoveLeft ();
- Screen & MoveRight ();
- Having seen these four functions, it goes without saying that they control the position of the cursor on the screen, that is: move the cursor up, move the cursor down, move the cursor to the left, and move the cursor to the right. If I now write them as overloaded functions, each is Screen & Move (); obviously it is not easy for programmers to understand. Therefore, for the use of function overloading, we should follow the logic of the application, rather than simply use it because it exists. Programmers should not force the use of overloaded functions.
- Have you ever wondered how the C ++ compiler determines which function in the overload you are calling? Even if their function names are the same. You may not hesitate to answer: it is through the function's parameter table. In fact, the recognition process is not as easy as you think. It involves parameter classification, parameter conversion, and many other aspects. I will explain them one by one below.
- Consider the following set of functions:
void S (); void S (int); void S (double, double = 1.2); void S (constchar *, constchar *); void Max (int, int); // intmain () { S (2.4); return; } //S(2.4); The call is the same as S (); S (int); S (double, double = 1.2); S (constchar *, constchar *), which are visible in the same domain.
- So good, the problem arises. S (2.4); which of the above four functions will be called?
- The first step for the compiler to determine an overloaded function is to determine the set of overloaded functions considered in the call. This set of functions is called a candidate function. The so-called candidate function is a function with the same name as the called function. The first four functions above can all be candidate functions (of course, there can be more than one), and only Max (int, int) is excluded.
- The second step of the compiler's judgment of an overloaded function is divided into two actions. The first action is that the compiler calls the viable function from the candidate functions selected in the first step. The number of function parameters of the feasible function is the same as the number of function parameters called (such as S (int)), or there can be more parameters of the feasible function, but the extra function parameters must have relevant default values (such as S ( double, double = 1.2);) The second action is to convert the arguments of the called function into the arguments of the candidate function according to the conversion rules of the parameter types. The principle here is to make full use of parameter type conversion, in other words, use parameter type conversion as much as possible. Of course, the conversion should take the candidate function as the target of the conversion. Only two of the above functions are feasible functions, and they are S (int); S (double, double).
- If no feasible function is found according to the parameter conversion rules, the call is an error, that is, no function matches the call, which is a no match function.
- The third step of the compiler's judgment of the overloaded function is to select the best match situation from the feasible functions selected in the second step. In the selection of the best feasible function, the conversion from the function argument type to the corresponding feasible function parameter must be graded, and the best feasible function is finally selected according to the ranking.