function overloading of C + + (function matching)

Source: Internet
Author: User

Case

void f (); void f (int); void f (intint); void F (doubledouble3.14);

Matching principle:

1) The number of formal parameters is equal to the number of arguments provided by this call

2) The type of each argument is the same as the corresponding parameter type, or the type of the forming parameter can be converted;

Find the best match:

1) The matching of each argument of the function is not inferior to that of other feasible function;

2) The match of at least one argument is better than that provided by other feasible functions;

Example:

A) F (2.56),b) F(d), C) F (0);d) F ( 3.9,9.8);

A) The feasible function is void f (int, int) with void f (double, double =3.2). However, the best match does not exist, considering that the first parameter void F (double, double =3.2) has a distinct advantage;

But considering the second argument 38 o'clock, void f (int, int) has obvious advantages and is ambiguous and rejects its request;

b) void f (int);

c) void f (int, int);

D) Double f (double, double = 3.2);

tips: When you overload a function, you should try to avoid forcing type conversions.

function matching and const arguments

Record (acount&); Record (const acount&); Const  // call record (const acount&); Record (b); // call Record (acount&);

function overloading of C + + (function matching)

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.