C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别

在看APUE Figure1.10的时候发现signal(SIGINT, sig_int)这里的sig_int直接用的函数名,但是看Thinking-in-C++ Vol.2的时候发现mem_fun(&Shape::draw)却对函数名进行了取地址操作,感觉有疑问就查了一下资料,下面的代码可以展示出这两者之间的一些区别

参考资料:

http://stackoverflow.com/questions/3050805/pointer-to-const-member-function-typedef

http://www.cplusplus.com/reference/functional/mem_fun1_t/

http://www.cnblogs.com/taobataoma/archive/2007/08/30/875743.html

http://www.cplusplus.com/reference/functional/mem_fun/

代码:

 1 #include <functional>
 2 #include <iostream>
 3 #include <algorithm>
 4
 5
 6 using namespace std;
 7
 8 void my(int arg);
 9
10 class MyClass
11 {
12 public:
13     void my(int arg) { cout << arg << endl; }
14 };
15
16 // 方法1,2
17
18 typedef void func_ptr(int);                 // func_ptr与func_ptr2本质是一样的,选择哪种定义方式看你的喜好
19 typedef void (*func_ptr2)(int);                // 参考:http://www.cnblogs.com/qrlozte/p/4439002.html
20 void dosomething_one(func_ptr ptr);
21 void dosomething_two(func_ptr2 ptr);
22
23 // 方法3,4,5
24
25 void dosomething_three(void (MyClass::*my_ptr)(int));
26
27 typedef void FuncType(int);
28 typedef FuncType MyClass::*MyClassFuncType;
29
30 typedef void (MyClass::*MemberFuncType)(int);
31
32 void dosomething_four(MyClassFuncType ptr);
33
34 void dosomething_five(MemberFuncType ptr);
35
36
37
38 int main() {
39     /*
40         方法1,2本质是一样的
41         方法3,4,5本质也是一样的
42     */
43     dosomething_one(my);
44     dosomething_two(my);
45     dosomething_three(&MyClass::my);
46     dosomething_four(&MyClass::my);
47     dosomething_five(&MyClass::my);
48     return 0;
49 } ///:~
50
51 void my(int arg)
52 {
53     cout << arg << endl;
54 }
55
56
57 void dosomething_one(func_ptr ptr)
58 {
59     ptr(1);
60 }
61
62 void dosomething_two(func_ptr ptr)
63 {
64     ptr(2);
65 }
66
67 void dosomething_three(void (MyClass::*my_ptr)(int))
68 {
69     MyClass *obj = new MyClass;
70     (obj->*my_ptr)(3);
71     delete obj;
72 }
73
74 void dosomething_four(MyClassFuncType ptr)
75 {
76     MyClass *obj = new MyClass;
77     (obj->*ptr)(4);
78     delete obj;
79 }
80
81 void dosomething_five(MemberFuncType ptr)
82 {
83     MyClass *obj = new MyClass;
84     (obj->*ptr)(5);
85     delete obj;
86 }
时间: 2024-08-29 17:10:46

C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别的相关文章

[C++] the pointer array &amp; the array&#39;s pointer

void main(){ // the pointer array char* arry[] = { "hello", "world", "haha" }; for (int i = 0; i < 3; i++){ printf("string:%s,address:%p\n", arry[i], arry[i]); } // the array's pointer int a[10] = { 0, 1, 2, 3, 4

函数指针(pointer to function)——qsort函数应用实例

一,举例应用 在ACM比赛中常使用 stdlib.h 中自带的 qsort 函数,是教科书式的函数指针应用示范. #include <stdio.h> #include <stdlib.h> int comp(const void*a, const void*b) { return *(int*)a - *(int*)b; } int main() { int n = 5; int *array = (int*)malloc(n*sizeof(int)); int i = 0; f

js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别

js中有两种声明函数的方法,分别为: var functionOne = function() { // Some code }; function functionTwo() { // Some code } 为什么会有两种不同的方法?每个方法的优点和缺点分别是什么?有什么情况是一种方法能完成而另外一种方法不能完成的吗? 答: by @Greg 不同点在于functionOne只会在到达赋值的那一行才会被真正定义,而functionTwo会在 包含它的函数或script脚本 执行的时候马上被定

Delphi深度探索之PItemIDList的基本概念

PIDL的秘密 从Windows 95开始,微软公司为操作系统引入了新的外壳界面,新的外壳从根本上改变了应用程序同操作系统的结合方式,遗憾的是微软公司对于发布同外壳相关的编程信息方面显得很吝啬,可以得到的资料非常少,而且质量也不高.对于Delphi开发者来说,情况就更为严重了,因为几乎所有的Windows API 文档都是针对C/C++程序员的,但是Nothing is impossible,在本文中,我们将开始外壳编程的历险,就让我们从PIDL开始吧. 外壳命名空间 新外壳系统中的一个核心概念

Delphi中的关键字与保留字

Delphi中的关键字与保留字 分类整理 Delphi 中的“关键字”和“保留字”,方便查询 感谢原作者的收集整理! 关键字和保留字的区别在于,关键字不推荐作标示符(编译器已经内置相关函数或者留给保留实现),二保留字是根本不可能作标示符(编译时有警示!) [系统保留字] and            array          as             asm begin          case           class          const constructor   

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

Delphi高手突破(四) Delphi高级进阶

别人造砖我砌房! Delphi  高手突破     VCL——Visual Component Library,是 Delphi 的基石.Delphi 的优秀,很大程度上得益于 VCL 的优秀.VCL 是 Delphi 所提供的基本组件库,也就是所谓的 Application Framework,它对Windows API(应用程序接口)进行了全面封装,为桌面开发(不限于桌面开发)提供了整套的解决方案,使得程序员可以在不知晓 API 的情况下进行 Windows编程.不过,作为专业的程序员,不知

Delphi -- Compiler helper for initializing/finalizing variable

1 it CompilerhelperForInitializingFinalizingVariable; 2 3 interface 4 5 { Compiler helper for initializing/finalizing variable } 6 7 procedure _Initialize(p : Pointer; typeInfo : Pointer); 8 procedure _InitializeArray(p : Pointer; typeInfo : Pointer;

Google C++ Style Guide----英文版

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354326> Background C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power

Delphi中的容器类

从Delphi 5开始VCL中增加了一个新的Contnrs单元,单元中定义了8个新的类,全部都是基于标准的TList 类. TList 类 TList 类实际上就是一个可以存储指针的容器类,提供了一系列的方法和属性来添加,删除,重排,定位,存取和排序容器中的类,它是基于数组的机制来实现的容器,比较类似于C++中的Vector和Java中的 ArrayList,TList 经常用来保存一组对象列表,基于数组实现的机制使得用下标存取容器中的对象非常快,但是随着容器中的对象的增多,插入和删除对象速度会