Function overloading function pointer met

Function overloading function pointers met
- when the names of overloaded functions assigned to the function pointer
  1. consistent selection and function pointers candidate list according to the rules of overloading;
  2. exactly matching function type and function type of function pointers candidate

Look at the following code:

#include <stdio.h>

int func(int x)
{
    return x;

}

int func(int x, int y)
{

    return x + y;
}

int func(int x, int y, int z)
{

    return x * y * z;
}

typedef int(*PFUNC)(int a);

int main()
{

    PFUNC p = func;
    int c = p(1);
    printf("c = %d\n",c);
    return 0;
}

Print Results c = 1

Such modifications do, typedef void (* PFUNC) (int a);

Such a compile-time error will occur,

 

 Overload met when the function described function pointer, the function types strictly match the function pointer to a function of the type of candidate

Note:
- Function Overloading must occur in the same scope
- the compiler needs with a function parameter list or select a function type . (The return value is not as a basis for overloading, function overloading met but when function pointers you need to consider the return value, and will strictly match)
can not directly obtain the entry address of an overloaded function by function name


Emphasize the same scope of reason:
because more than a scope in C ++, and C language is only a scope - global scope.

Look at the following example:

#include <stdio.h>

int func(int x)
{
    return x;

}

int func(int x, int y)
{

    return x + y;
}


int main()
{

    printf("%p\n",func);
    printf("%p\n",func);
    return 0;
}

 

 该程序说明,无法直接通过函数名得到重载函数的入口地址

Guess you like

Origin www.cnblogs.com/-glb/p/11892611.html