c ++ study notes (XI)

Function overloading (overloading)

concept

Overloads function is a special case, for ease of use, C ++ statements to allow several functions with the same name in the function similar to the same range, but the same name as a function of these parameters form (refer to the number of parameters, the type or order) must be different , that perform different functions with the same function. This is overloaded functions.

Overloaded function used to implement the functions and the like are different types of data processing problems. Not only function return values ​​of different types.

Understand: that there are multiple functions of the same name, but their list of the different parameters.

form

Such as

void fun(int );
void fun(float,float);

While we fight the same code name, but the compiler at compile time they add signature to be renamed.

In the above example will rename at compile time:

fun_int(int );
fun_float_float(float ,float);

This is the principle function overloading.

So when the compiler will first overloaded functions are renamed.

This is called overloading resolution.

Use:
function overloading as the automatic matching system so you can solve a variety of data types of calculations can be used to solve many preset function, the more trouble you call, bad call and other issues.

note

one example:

void fun(int a);
void fun(int& ra);

These two parameters are passed in exactly the same, because the reference equivalent to the body, so the compiler will complain that this can not be said to be overloaded functions.

The default parameters

concept:

Refers to the default parameters when a function call is omitted automatically when the value of the argument. For example, if the void wow (int n) is provided with a default value of 1 to n, wow calling the function () corresponds to wow (1). This greatly increases the flexibility to use the function.

usage

void fun(int a=1, int b=3, int c=5);
In this way, we refer to this function when you declare that you do not give it any arguments it will use the default parameters.

note:

1. Default parameters can be set only once in the function declaration. Only when no function declaration can only be set in the function definition.

2. The order of default parameters defined from right to left. That is, if a parameter setting default values, its right argument should have default values.

3. default arguments, the argument follows the calling sequence, from left to right by-call. This is to make it clear to point 2, do not be confused.

Guess you like

Origin www.cnblogs.com/147258369k/p/12288393.html
Recommended