函数指针
参考资料
typedef int (*MYFUN)(int, int)
1 2 3 4 5 6 7 8 9 10 11
| #pragma once #ifndef FUNC_PTR_H #define FUNC_PTR_H typedef int (*pfint)(int); int func1(int a); int func2(int a);
int* func3(int &a);
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include "func_ptr.h" #include<iostream> using namespace std; int func1(int a) { cout << "func1: " << a << endl; cout << "&func1: " << &func1 << endl; return a; }
int func2(int a) { cout << "func2: " << a << endl; return a; }
int* func3(int &a) { cout << "func3: " << a << endl; return &a; }
|
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
| #include "func_ptr.h" int main() { func1(1); func2(2); int (*pfunc)(int) = &func1; cout << pfunc << endl; (*pfunc)(3); pfunc = func2; cout << pfunc << endl; (*pfunc)(4); pfunc = &func2; (*pfunc)(5); pfunc(6); pfint pfint1; pfint1 = func1; pfint1(2); int (*funcArr[2])(int); funcArr[0] = func1; funcArr[1] = func2; funcArr[0](1); funcArr[1](2); return 0; }
|
可以new出来函数指针吗?没找到有这么写的。