Comparison between C ++ and C #: function (4) heavy-load function matching

Source: Internet
Author: User
Document directory
  • Benefits
  • Notes
Comparison between the matching of overload functions and the matching of beautiful guys

We know that the number of parameters in the overload function is the same, but the type is different. c ++ allows more flexible implicit type conversion. so how to accurately match the desired function when calling the function is a problem.

At this time, it is generally divided into three steps to match

(1) Find out all the overloaded functions first. select the same number of parameters first. if the number of parameters is different, remove them directly. this is equivalent to Hai Xuan. For example, if a beauty looks for a husband, first look at the first feeling, if the first feeling is not good. you have no chance to go to the Shanghai election.

(2 ). then, the parameters are matched from the functions with the same parameters. if you find a matching one, you will be done. it's not that you're not married if you find something rich, handsome, and appealing to a pretty girl. but the ideal is beautiful, the reality is cruel, and sometimes it is not so perfect. it may be handsome, but it is also difficult. or you can lose money, but it's not as ugly as humans.

(3) If the exact match is not found, implicit type conversion is performed first. if it matches the unique one, it will be OK. there may be only one rich person, or one handsome guy. so you don't have to choose.

(4) How many matches are there after type conversion? You need to make a decision based on a rule, and choose the one that matches the best. this is just like a pretty girl in a few handsome and rich, and finally found a very good money, but still barely can be regarded as a person, not as a wild person or a gorilla. so OK

(5) In (4), if matching is performed according to the rules, two or more matching results are not available. this is a computer dumb. an error is reported directly. however, people may be smart. when the beauty is hesitant, she may ask for advice from girlfriends. or you can't catch it. you can't hesitate to let the cooked duck fly. this is what smart women do, but there are women with big breasts and no brains. hesitated and hesitated. As a result, all the male men were turned and ran away by other women's goblin. by then, the year's appearance will no longer have to be bent to marry a man who could not see him before. in the future, the man may occasionally wear a green hat.

 

C # is used as an example.

Assume there are five overload functions as follows:

Void fun (string name) {}( 1)

Void fun (INT num) {}( 2)

Void fun (long num) {}( 3)

Void fun (short SH, long on) {} (4)

Void fun (long on, short SH) {}( 5)

 

Then compare the five matching cases mentioned above.

(1) Fun ("Arwen"); // match void fun (string name ){}

In this case, select the functions 1, 2, 3 as the candidates, and then directly match (1), So OK

(2) Short num = 211;

Fun (Num); // match void fun (INT num ){}

It also first matches 1, 2, 3, and then fails to find the short type. therefore, implicit conversion is performed. short can be converted to Char, Int, long. therefore, (2) and (3) are matched ). in this case, you need to select the most appropriate one according to the rules. generally, the best choice is the number of bytes with the least difference. short is 2 bytes, Int Is 4, long is 8. therefore, short is converted to int optimized. so select "OK" (2 ). of course, if there is no function (2), you should select (3 ).

(3) int num = 918;

Fun (Num, num); // matching error. No suitable one found.

It first matches (4) and (5), and then converts the int type to short and long respectively to match (4), and converts the int type to long, respectively, shot matches (5 ). then, determine which matching is the best. the result is dumb. the two seem to be the same.

The call is ambiguous between the following methods or proerties.

 

C # Since implicit type conversion is strictly controlled, it can be switched only when there is no loss of precision during conversion. therefore, it is relatively simple. c ++ can implicitly convert any numeric type. in this case, matching is more troublesome. more complex.

However, the principle is the same.

 

Benefits and considerations of heavy-duty functions

The benefit of the overload function is that it saves us a lot of trouble to name a function like a function. it is also easy to use. You only need to remember a function name. During the call, the development environment tool will prompt you with the parameters to be passed in.

Overload functions are more useful in C #. For example, if a function needs to pass many parameters, but some parameters are not important, the user does not care about them. just set a default value. this requires the use of overload functions so that users can choose to pass different parameters. in C ++, the function of default real parameters is available, so the demand for heavy-load functions is less important.

Notes

However, through the above example, we found that a matching error may occur if there are more than one function overload. therefore, if the overloaded function is prone to this situation, for example, several parameters are of the numerical type. you can choose not to use the overload function. this is from the perspective of writing overload functions.

From the perspective of function users, when calling a function, we need to see what parameters are required by the function. If we do not pass a completely matched parameter, we need to explicitly convert it by ourselves. and then upload the file.

 

 

Function pointer matching

In C ++, there is a function pointer concept. When we declare a function pointer, then the matching rules are exactly the same as above.

However, C # does not have the pointer concept, but C # has a mechanism called a proxy. it is a bit similar to a function pointer, so it is too dominant. If it cannot be completely matched, an error is reported directly, so no matter what, implicit conversion and then matching.

Example

Delegate void mydelegate (short SH); // defines a proxy

Then there are functions

Void fun (INT num ){}

Declare a proxy type and initialize. mydelegate Arwen = fun;

Short SH = 12;

Arwen (SH); // an error is reported. The proxy indicates that it points to a function with the return value of void and the form parameter of short type. However, you are an int type function, so it cannot match.

Related Article

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.