C++函数指针 各个方式表现 和 使用方法

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 
 6 void test()
 7 {
 8     cout << "test with no arguments\n";
 9 }
10 void test2(int a)
11 {
12     cout << "test with one arguments "<< a <<"\n";
13 }
14 void* test3()
15 {
16     cout << "test with no argumetens and return"<<"\n";
17     int a =10;
18     return (void*)&a;
19 }
20 void *test4(int b)
21 {
22     cout <<"test with one arguments and return pointer"
23     << "\n";
24     return (void*)&b;
25 }
26 
27 
28 extern int main01() {
29     typedef void (*pf)();
30     pf _func_01 = test;
31     _func_01();
32 
33     typedef void (*pf_one)(int);
34     pf_one _func_02 = test2;
35     _func_02(2);
36 
37     typedef void *(*pf_sce)();
38     pf_sce _func_03 = test3;
39     cout << *((int *) _func_03()) << endl;
40 
41     typedef void *(*pf_sce_one)(int);
42     pf_sce_one _func_04 = test4;
43     cout << *((int *) _func_04(5)) << endl;
44 }

 

posted @ 2016-06-12 16:00  岑小良  阅读(180)  评论(0编辑  收藏  举报