Detailed explanation of C + + function overloading

Detailed explanation of C + + function overloading

In actual development, sometimes we need to implement several functions with similar functions, but some details are different. For example, we want to exchange the values of two variables. These two variables have many types, such as int, float, char, bool, etc. we need to pass the address of the variable into the function through parameters. In C language, programmers often need to design three functions with different names, and their function prototypes are similar to the following:

void swap1(int *a, int *b);      //Swap values of int variables
void swap2(float *a, float *b);  //Swap the value of the float variable
void swap3(char *a, char *b);    //Swap values of char variables
void swap4(bool *a, bool *b);    //Swap values of bool variables

But in C + +, this is completely unnecessary. C + + allows multiple functions to have the same name as long as their parameter lists are different. This is Function Overloading. With overloading, a function name can be used for many purposes.

Parameter list is also called parameter signature, including the type, number and order of parameters. As long as there is one difference, it is called different parameter list.

[example] exchange the values of different types of variables with the help of function overloading:

#include <iostream>
using namespace std;
//Swap values of int variables
void Swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}
//Swap the value of the float variable
void Swap(float *a, float *b){
    float temp = *a;
    *a = *b;
    *b = temp;
}
//Swap values of char variables
void Swap(char *a, char *b){
    char temp = *a;
    *a = *b;
    *b = temp;
}
//Swap values of bool variables
void Swap(bool *a, bool *b){
    char temp = *a;
    *a = *b;
    *b = temp;
}
int main(){
    //Swap values of int variables
    int n1 = 100, n2 = 200;
    Swap(&n1, &n2);
    cout<<n1<<", "<<n2<<endl;
   
    //Swap the value of the float variable
    float f1 = 12.5, f2 = 56.93;
    Swap(&f1, &f2);
    cout<<f1<<", "<<f2<<endl;
   
    //Swap values of char variables
    char c1 = 'A', c2 = 'B';
    Swap(&c1, &c2);
    cout<<c1<<", "<<c2<<endl;
   
    //Swap values of bool variables
    bool b1 = false, b2 = true;
    Swap(&b1, &b2);
    cout<<b1<<", "<<b2<<endl;
    return 0;
}

Operation results:

200, 100
56.93, 12.5
B, A
1, 0

The reason why this example uses the function name swap instead of swap is that the C + + standard library has provided a function to exchange the values of two variables. Its name is swap, which is located in the algorithm header file. In order to avoid conflict with swap in the standard library, this example specially writes S.

Since the standard library has provided the swap() function, why should this example implement it by itself? Isn't it laborious and thankless? Exchanging the values of two variables is a classic and practical case of function overloading. This example is only for teaching demonstration, not to replace swap() in the standard library. Readers should also insist on using swap() in the standard library in the future coding process.

Through this example, we can find that overloading is to have multiple functions with the same name but different parameters in a scope (the same class, the same namespace, etc.). The result of overloading is that a function name has multiple uses, making naming more convenient (in medium and large projects, naming variables, functions and classes is a troublesome problem), and calling more flexible.

When using overloaded functions, the functions of functions with the same name should be the same or similar. Do not use the same function name to realize completely irrelevant functions. Although the program can also run, its readability is not good, which makes people feel inexplicable.

Note that different parameter lists include different number, type or order of parameters, and only different parameter names are not allowed. The return value of a function cannot be used as the basis for overloading.

Rules for overloading functions:

Function names must be the same.

The parameter list must be different (different number, different types, different order of parameters, etc.).

The return types of functions can be the same or different.

Just different return types are not enough to overload functions.

How does C + + overload functions

C + + code will rename the function according to the parameter list during compilation. For example, void Swap(int a, int b) will be renamed to_ Swap_int_int, void Swap(float x, float y) will be renamed to_ Swap_float_float. When a function call occurs, the compiler will match one by one according to the incoming arguments to select the corresponding function. If the matching fails, the compiler will report an error, which is called Overload Resolution.

Different compilers have different renaming methods. This is just an example. This may not be the case.

From this point of view, function overloading is only at the syntax level. In essence, they are different functions, occupy different memory, and the entry address is also different.

Keywords: C C++

Added by Marsha on Wed, 02 Feb 2022 13:19:03 +0200