C++ function overloading problem

Function prototype: return type + function name + parameter list (parameter type and number of parameters)

So the question is: why does C++ allow function overloading? And the C language can not?
The reason is:
C language distinguishes functions according to the function name. If we have two functions with the same function name, but different return types or parameters, the compiler will add "_" to the function name when compiling. Rename, so the function name generated in the symbol table is the same, then the C language compiler cannot determine which function it is.
C++ does name renaming rules (name smashing). According to the function name, parameter list (including the number of parameters and parameter types) to judge.
insert image description here
When overloading a C++ function, you need to pay attention to the following points:
1. The return type cannot be used as the basis for function overloading.
insert image description here

2. If the return type, function name, and parameter types are the same, but the number of parameters is different, overloading may not be possible. If there is no default value or default value, it can be overloaded. If there is a default value, it cannot be determined.
insert image description here

Therefore, it is impossible to determine which function is called when calling, so pay attention to the ambiguity in the above two cases

Supongo que te gusta

Origin blog.csdn.net/m0_54355780/article/details/122892224
Recomendado
Clasificación