c++@ overloaded functions

Source: Internet
Author: User
Tags pow

For a detailed analysis reference for overloading:

Http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html

The internal mechanism involves how overloaded functions resolve naming conflicts and invoke matching problems.

Criteria for resolving overloads:

    • exact match : parameter matching without conversion, or just trivial conversions, such as array name to pointer, function name to pointer to function, t to const t;
    • Promotion matches : integer promotion (such as bool to int, char to int, short to int), float to double
    • use standard conversion matching : such as int to double, double to int, double to long double, derived* to base*, t* to void*, int to unsigned int;
    • use user-defined matches ;
    • use ellipsis to match : similar to the ellipsis parameter in printf

If multiple matching functions are found at the highest level, the call is rejected (because of ambiguity, ambiguous). Look at the following example:

void print (int), void print (const char*), void print (double), void print (long), void print (char), void H (char c,int i,short s , float f) {     print (c);//exact match, call print (char)     print (i);//exact match, call print (int)     print (s);//integer promotion, call print (int)     print (f);//float to double lift, call print (double)     print (' a ');//exact match, call print (char)     print (49);//Exact Match, Call print (int)     print (0);//exact match, call print (int)     print ("a");//exact match, call print (const char*)}

Defining too few or too many overloaded functions can lead to ambiguity and look at one of the following examples:

void F1 (char), void F1 (long), void F2 (char*), void F2 (int*), void K (int i) {       F1 (i);//Call F1 (char)? F1 (Long)?       F2 (0);//Call F2 (char*)? F2 (int*)? }

At this point, the compiler will error, throw the errors to the user to handle: by the display type conversion to call and so on (such as F2 (Static_cast<int *> (0), of course, it is ugly, and you want to invoke other methods when you use to convert). The above example is just a case of a parameter, let's look at a two-parameter scenario:

int pow (int, int);d ouble pow (double,double), void G () {       double D=pow (2.0,2)//Call POW (int (2.0), 2)? Pow (2.0,double (2) )?}

/* * Overloading: Two or more functions have the same function name, different number of parameters. * When the number of arguments is the same, the parameter type is different, and the overload will call the function of the type parameter first, * If not, cast the type. */#include <iostream>using namespace Std;int Ave (int n1, int n2);d ouble Ave (double n1, double n2);d ouble Ave (double N1, double N2, double n3); int main () {cout << "the average of 2.0 2.5 3.0 is:" << Ave (2.0, 2.5, 3.0) <<e  Ndl;cout << "The average of 4.5 5.5 is:" << Ave (4.5, 5.5) << endl;cout << "The average of 5 8 is:" << Ave (8, 5) << Endl;return 0;} int Ave (int n1, int n2) {return (N1-N2);} Double Ave (double n1, double n2) {return ((N1+N2)/2.0);} Double Ave (double n1, double N2, double n3) {return ((N1+N2+N3)/3.0);}
Operation Result:
The average of 2.0 2.5 3.0 Is:2.5the average of 4.5 5.5 is:5the average of 5 8 is:3

  

[email protected] overloaded functions

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.