Detailed introduction to the application of overload functions in C ++

Source: Internet
Author: User

Previously, we introducedC ++You can refer to this article to learn more about the heavy-duty operators in C ++. Today we will introduce youOverload Functions.

Overloaded function) is a special function supported by C ++. The C ++ compiler judges function overloading as one of the most complex functions in C ++.

First, let's clarify the definition of the overload function: the function names in the same declaration domain are the same, while the parameter tables are different, this is a special function that uniquely identifies a function through the parameter table of the function and distinguishes it.

You may want to ask, why does a function need to be reloaded? When should I choose function overload function overloading? This is what I will introduce below.

When you want to define a group of functions so that they can perform a series of operations, but they are applied to different parameter types. In this case, you can select the overload function.

For example:

 
 
  1. Int z_x_max (int, int); // returns the maximum values of two integers;
  2. Int ve_max (const vector <int> &); // returns the maximum value in the vector container;
  3. Int matrix_max (const matrix &); // returns the maximum value referenced by matrix;

The above three functions can be roughly used to judge the maximum value in a group of numbers. For function users, they do not care about the details of function definitions, that is to say, they do not care about determining the size of two integers and determining the size of the array vector container. Different functions should be used, but this is something that programmers have to think. Programmers must remember and search for each function name. Function overloading frees programmers from the complexity of such a problem. C ++ provides such support. The above three comparison functions can be defined:

 
 
  1. Int Max (int, int); // returns the maximum values of two integers;
  2. Int Max (const vector <int> &); // returns the maximum value in the vector container;
  3. Int Max (const matrix &); // returns the maximum value referenced by matrix;

Yes! Different functions can be distinguished at a glance through parameters.

At the same time, function Overloading is not applicable. For example, a series of functions that control the cursor are involved in the development of a text editor:

 
 
  1. Screen& MoveUp( );  
  2. Screen& MoveDown( );  
  3. Screen& MoveLeft( );  
  4. Screen& MoveRight( ); 

It is self-evident that the four functions control the position of the cursor on the screen, that is, move the cursor up, move the cursor down, move the cursor left, and move the cursor to the right. If I write them as overload functions now, each of them is Screen & Move (); obviously not easy for programmers to understand. Therefore, the use of function overloading should follow the application logic, rather than simply using it because of its existence. Programmers should not barely use overload functions.

Have you ever wondered how the C ++ compiler determines which function you are calling in the heavy load? Even if their function names are the same. You may not hesitate to answer: it is through the function parameter table. In fact, the process of identification is not as easy as you think. It involves the classification of parameters, parameter conversion, and many other aspects. I will explain them one by one.

Assume that the following functions are available:

 
 
  1. Void S ();
  2. Void S (int );
  3. Void S (double, double = 1.2 );
  4. Void S (const char *, const char *);
  5. Void Max (int, int );
  6. //......
  7. Int main ()
  8. {
  9. S (2.4 );
  10. Return;
  11. }
  12. // S (2.4); call with S ();
  13. S (int); S (double, double = 1.2); S (const char *, const char *), the declaration is visible in the same domain.

So well, 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 function set is called the candidate function candidant 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, they can be multiple), but Max (int, int) is excluded.

The second step for the compiler to judge the overloaded function is divided into two actions. The first action is for the compiler to call up the feasible function viable function from the candidate function selected in the first step ). The number of feasible function parameters is the same as the number of called function parameters, for example, S (int), or the number of feasible function parameters can be greater, however, the extra function parameters must have relevant default values, such as S (double, double = 1.2 );) the second action is to convert the called function real parameters to conversion according to the conversion rules of the parameter type) to the real parameters of the candidate function. Here, we use the principle of making full use of parameter type conversion. In other words, we try to use parameter type conversion as much as possible. Of course, the conversion should be based on the candidate functions. Only two of the above functions are feasible functions, which are S (int); S (double, double ).

If no feasible function is found according to the parameter conversion rules, the call is incorrect. In this case, no function matches the call and no match function is returned ).

The third step for the compiler to judge the overloaded function is to select the best feasible function best match situation from the feasible function selected in the second step ). In the selection of the best feasible function, the conversion from the real parameter type of the function to the corresponding feasible function parameters must be classified by rank, ranked according to the rank), and finally the best feasible function is selected. The best feasible function is the function to be called by the compiler.

I hope this article will help you.

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.