Function overload analysis

Posted by ElectricMessiah on Sun, 21 Nov 2021 00:24:56 +0100

Analysis of function overloading (I)
Concept of overload:
The same identifier has different meanings in different contexts

Function overloading in C + +
Function overload:
Define different functions with the same function name
When the function name is matched with different parameters, the meaning of the function is different

int func(int x)
int func(int x, int y)
int func(const char* s)
{
	return strlen(s);
}

Function overloading meets at least one of the following conditions:
(1 * *) different number of parameters**
(2) Different parameter types
(3) The order of parameters is different

int func(int a, const char* s)
{
    return a;
}
int func(const char* s, int a)
{
    return strlen(s);
}

What happens when a function default parameter encounters a constructor?

#include <stdio.h>

int func(int a,int b,int c = 0)
{
    return a*b*c;
}
int func(int a,int b)
{
    return a+b;
}

int main()
{
    int c = func(1,2);  //error, the compiler does not know which function to call

    return 0;
}

Guidelines for compiler calls to overloaded functions
All functions with the same name are candidates
Try to find feasible candidate functions
Exact match argument
Arguments can be matched by default parameters
Matching arguments by default type conversion
Matching failed:
If the candidate function finally found is not unique, ambiguity occurs and compilation fails
Unable to match all candidates, function undefined, compilation failed

Considerations for function overloading
Overloaded functions are essentially independent functions
Overloaded functions have different function types
The return value of a function cannot be used as a basis for function overloading
Function overloading is determined by function name and parameter list

Programming experiment: the essence of function overloading

#include <stdio.h>

int add(int a, int b)  //int(int, int)
{
    return a+b;
}
int add(int a, int b, int c)  //int(int, int, int)
{
    return a+b+c;
}

int main()
{
    printf("%p\n",(int(*)(int, int))add);        //The function name represents the entry address of the function
    printf("%p\n",(int(*)(int, int, int))add);

    return 0;
}


The results show that the essence of function overloading is independent and different functions

Function overload analysis (Part 2)
The following function pointer will store the address of which function

Overload and pointer
Function overload encountered a function pointer
When assigning an overloaded function name to a function pointer
1. Select candidates consistent with the function pointer parameter list according to the overload rules
2. Strictly match the function type of the candidate with the function type of the function pointer

#include <stdio.h>
#include <string.h>
int func(int x)
{
    return x;
}

int func(int a, int b)
{
    return a + b;
}

int func(const char* s)
{
    return strlen(s);
}

typedef int(*PFUNC) (int a);
//typedef double(*PFUNC) (int a);  //errro, the type pointed to by the function pointer is different from the above three functions
int main()
{
    int c = 0;
    PFUNC p = func;
    c = p(1);

    printf("c = %d\n",c);
    return 0;
}

Overload and pointer
be careful:
Function overloading must occur in the same scope
The compiler needs to make function selection with parameter list or function type
The entry address of an overloaded function cannot be obtained directly from the function name****

C + + and C call each other
In practical engineering, it is inevitable that C + + and C code call each other
C + + compiler can be compatible with the compilation mode of C language
The C + + compiler takes precedence over the C + + compiler
The extern keyword can force the C + + compiler to compile in C mode

extern "C"
{
    //do C-style compilation here
}

How to ensure that a piece of C code will only be compiled in C? (how to ensure that a piece of code can pass both in g + + and gcc compiler)
Solution
__ cplusplus is a standard macro definition built into the C + + compiler
__ The meaning of cplus plus
Ensure that the C code is compiled into the object file in a unified C way

#ifdef __cplusplus
extern "C" {
#endif
    #include "add.h"
#ifdef __cplusplus
}
#endif

Programming experiment: C calls C + + functions

matters needing attention:
(1) The C + + compiler cannot compile overloaded functions in C
(2) The compilation method determines that the function name is marked after compilation
C + + compilation method compiles the function name and parameter list into the target name
C compilation mode only compiles the function name as the target name

Summary:
Function overloading is an important upgrade of C + + to C
Function overloading distinguishes different functions with the same name through function parameters and function list
extern keyword can realize the mutual call between C and C + +
The compilation method determines the final target name of the function name in the symbol table