函数重载和函数指针
函数重载与函数指针
当使⽤用重载函数名对函数指针进⾏行赋值时
根据重载规则挑选与函数指针参数列表⼀一致的候选者
严格匹配候选者的函数类型与函数指针的函数类型
函数指针,调用的时候是不能够发生函数重载的
函数指针基本语法
1 2 3 4 5 6 7 8 9 10 11 12
| int func(int a, int b,int c) { cout << "func2" << endl; return 0; }
typedef int (My_func)(int, int);
typedef int(*My_func2)(int, int);
int(*fp3)(int, int) = func;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
void func(int a, int b) { cout << a << b << endl; }
void func(int a, int b, int c) { cout << a << b << c << endl; }
void func(int a, int b, int c, int d) { cout << a << b << c <<d << endl; }
typedef void(myfunctype)(int, int);
typedef void(*myfunctype_pointer)(int, int);
int main(void) { myfunctype * fp1 = NULL;
fp1 = func;
fp1(10, 20);
myfunctype_pointer fp2 = NULL; fp2 = func; fp2(10, 20);
void(*fp3)(int, int) = NULL;
fp3 = func;
fp3(10, 20);
cout << " -----------------" << endl;
fp3(10, 20);
void(*fp4)(int, int, int) = func;
fp4(10, 10, 10);
void(*fp5)(int, int, int, int) = func; fp5(10, 10, 10, 10); return 0; }
|