C + + Select overloaded functions

Source: Internet
Author: User
Tags function definition

First, function overloading

The key to common function overloading is the parameter list ---also called function feature labels. overloading can occur in the following cases in a function parameter:

1. Different number of parameters

2. Different types of formal parameters

3, the type and number of formal parameters are different

Const formal parameters and function overloading

overloads cannot differentiate between const and non-const

First, what is the top-level constand what is the underlying const

If there is a pointer or reference, pointing to a data, the top-level const indicates that the pointer cannot be changed (it cannot be re-directed to another data), and the underlying const indicates that the data pointed to by the pointer cannot be changed. (Top-level const can be understood as different types)

The bottom-level const notation:

Int num = 1;

const INT * p = # equivalent to int const * p = &num

Top-Level const notation:

Int num = 1;

int * Const P = #

The top-level const can be distinguished when the pointer is a function argument, while the underlying const is indistinguishable

The following syntax appears as a repeating definition:

Double cube (double x)

Double cube (const double x)

The underlying const below can distinguish between

void dribble (char* bits);

void Dribble (const char * bits);

The following top-level const is indistinguishable:

void dribble (char* bits);

void Dribble (char * const bits);

In the same vein, the case of references can be distinguished

Double cube (double& x)

Double cube (const double & x)

Lvalue and Rvalue parameters

The first is to distinguish between a reference and a copy of the value, the following two definitions will appear duplicate definitions

Double cube (double& x);

Double cube (double x);

Left and right parameter conditions differ

The hypothesis is defined as follows

Double cube (double&& x)

Double cube (double x)

The second is called when using cube (x), but there are multiple matching problems when using cube (x+1) (the matching problem indicates that the function definition is unambiguous, but in finding the corresponding matching function there are multiple results that match the criteria, just not knowing which one to choose)

Two, function matching problem

After defining a set of overloaded functions, we need to invoke them with reasonable arguments. function matching refers to a process in which we associate a function call with one of a set of overloaded functions, and function matching is also called function determination. The compiler first compares the parameters of each function in the called real-participation overload collection, and then determines which function to invoke based on the result of the comparison.

What we need to master now is that there are three possible results when calling overloaded functions:

    • The compiler finds a function that best matches the argument and generates the code that invokes the function;
    • No function could be found to match the invoked argument, at which point the compiler emits an error message with no matching;
    • More than one function can match, but each one is not the obvious best choice. An error also occurs, called a ambiguity call.

The function matching process is divided into three steps

1, to determine the candidate function, the candidate function has two characteristics: first, the function is called with the same name, and the second is that its declaration is visible at the point of invocation.

2, determine the feasible function, the feasible function also has two characteristics: first, the number of formal parameters is the same as the number of formal parameters of this call, and the second is that each argument type is the same as the corresponding parameter type, or can convert the type of forming parameter

3, to find the best match, there are a number of feasible functions, it is necessary to choose a best match, the basic idea is that the actual parameter type and formal parameter type, the closer they match the better.

function matching with multiple parameters, and only one function satisfies the following conditions, the function matches successfully:

L The function does not match each argument to a match that is required by other viable functions

L At least one argument match is better than other feasible functions

For example, there is a conflict when using F (2,3.5) to match the following function:

void f (int x,int y)

void f (Double x, double y)

Argument type conversions

The compiler divides the conversion of arguments into formal parameters into several levels

1, accurate matching, including the following conditions

l Real Participation formal parameter types are the same

L arguments are converted from an array type or function type to the corresponding pointer type

L Add a top-level const to an argument or remove the top-level const from a real argument

2. Matching by const conversion

3. Conversion through type promotion

4, by the arithmetic type (int, short and so on conversion) or pointer conversion (0 or literal value nullptr can be converted to any pointer type, pointing to any very amount of pointers can be converted to void*, pointers to any object can be converted to void*)

5. Matching by class type conversion

Difference type of promotion and conversion:

converting char, unsigned char, short, unsigned short to int type is called type promotion (promotion)

A long double, double, float, unsigned long, long long, unsigned long, long, unsigned int, and int conversions are called type conversions

Type rank from highest to lowest: long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int, int

distinguish "ignore top-level const" "and" Const Conversion "

Ignoring the example of the top-level const, they can be called by double x = 9.0;cube (x), two statements (even if X is non-const, but can still call double cube (const double x)), so the following two are the same definition:

Double cube (const double x)

Double cube (double x)

To see an example of a pointer, here are two examples of ignoring the top-level const. They can be separated by double x = 9.0;cube (x); two statement invocation:

Double cube (double* x)

Double cube (double* const x)

If there is a double cube (const double x), it can also be called with the following statement because

Double x = 9.0

Cube (&X)

These are examples of top-level collisions, but the const conversion differs, which means that non-const objects can be converted to const

Double cube (const double * x)

Can be called by the following statement

Double x = 9.0

Cube (&X)

X is not const, but is passed to const and there is a const conversion

C + + Select 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.