definition of C + + function Overload: 
In the same scope class , a set of functions have the same function name , the argument list is different (the number of parameters different/parameter types are different), the return value can be different from two, function overload function:
 
Overloaded functions are often used to name a set of function-like functions in the same scope using the same function name, which reduces the number of function names, avoids the contamination of namespaces, and has great benefits for the readability of the program. third, function overload is a static polymorphism:
 
(1) Polymorphism: Expressing a different form with the same thing;
(2) Polymorphism is divided into:
Static polymorphism (polymorphic at compile time)
Dynamic polymorphism (polymorphic at runtime);
(3) Function overload is a kind of static polymorphism; four, C + + function overload principle:
 
A
When the compiler compiles a function of the same name in the scope that is currently in use in the . cpp file , the function is renamed based on the type and order of the function parameters (different compilers do not have the same renaming criteria for functions at compile time) but overall, they renamed the same function name in the file; in the VS Compiler:
Renamed and recorded in the map file based on the return value type (not conclusive) + parameter type and order (decisive) rules. in the linux g++ compiler:
Rename the record in the symbol table according to the number of characters of the function name + the parameter type and the order rule; so as to produce different function names, when the outside function is called, it is according to the result of this record to find the function name that conforms to the requirement, make the call; why can't C language implement function overload
 
when the compiler compiles the. c file , the function is simply renamed, and the specific method is to add "_" to the function name, so the function name that joins the two function names is the same as the function names after the compilation; the caller makes an error because he does not know exactly what to call;  Experiment Description:
 
(a) in C, if the function name of two functions is the same, regardless of the formal parameter and return value is the same, the program will run an error
In the. c file, write the following code, and run an error in VS2008:
 
#include <stdio.h>
int Add (int a,int b)
{return
   a+b;
}
Float ADD (float a,float b)
{return
    a+b;
}
int main ()
{
    Add (10,20);
    ADD (20.0f,30.0f);
    return 0;
}
 
Run Result: ERROR-function "int Add (Int,int)" already has body
Error Reason:
Program from compile to run out of the results of several stages. One of the stages refers to generating a symbol table .
Let's take a look at the symbol table for the function above. The symbol table is in the. map file, and the symbol table file is not displayed by default in VS. To display it, set this:
Project name right-click-> Properties--> Configuration Properties--> linker-–> Debug--> Generate mapping file-> Select Yes;
There are two functions with the same name in the C program, the compiler does not pass (Error: Add function already has a body) can not generate symbol table. So, I remove a function, let the program compile, and see the name of the function in the symbol table
From this we can conclude that theadd () function in the C program is renamed in the symbol table by an underscore in the front of the function name. So if a function of the same name appears in a C program, they are named in the symbol table, and then there is a conflict when the call occurs.
 
(ii) Change the above code to a. cpp file, and then view the names of the two add () functions in the map after compilation;
Here we can see that in the. cpp file, the function names of the two functions are the same, but the names they generate in the symbol table are different.
‘。 ' Indicates the beginning of the name, '. ' The function name ' @ @YA ' means that the parameter table begins, and the 3 characters that follows represent the return value type and two parameter types. "@z" indicates the end of the name.
Because the names in the symbol tables generated by two functions in the. cpp file are different, they can be compiled.
 
So since the C + + compiler and C compiler have different renaming rules for function names;
So how do you invoke the. c file's function in a. cpp file.
A related article written in the past
(c) Different compilers differ in the rules for renaming C + + code functions;
Below look at Linux g++ compiler to rename the same code function name;
 
CPP files need to be compiled with g++ in a Linux virtual machine.
installing g++ is simple. Command: Yum install gcc gcc-c++
Once installed, it will be OK. After running the program, use the command:objdump a.out-t > Test.out
- T is the symbol table , and finally the generated symbol table is placed with the redirection symbol in the test.out file . When you open the Test.out file, you will see that in the symbol table generated by the function add (int a,int b) that the integer number is added, the Add function name is recorded as _z3addiii, where. _z represents the beginning of a symbol table name , 3 represents the number of characters in the function name , andIII represents the type of three formal parameters in order;
As can be seen: the same. cpp file in the Windows vs2008 compiler and Linux g++ compilation, the same function name after the compilation of the rename is not the same;