在c++中,使用函数指针的时候,我一般使用静态成员函数的指针。另外,还有一种普通成员函数的指针,我用的比较少。今天写了一个测试代码,测试其功能
先说结论
结论 1
静态成员函数的指针是真正的函数指针(即地址)
普通成员函数的指针是函数偏移量(不是函数地址)
结论 2(非静态)
对于普通的成员函数指针,偏移量是全局的。编译器解析的时候,可以识别为具体的函数地址
对于虚函数的成员函数指针,偏移量是相对于类的虚函数表的。解析的时候,识别为相对于虚函数表的偏移量
Object和Event代码
14 class Object
15 {
16 public:
17 Object() { printf("Object construct, %p\n", this); }
18 ~Object() { printf("Object deconstruct, %p\n", this); }
19
20 public:
21 void foo() {
22 printf("Object::foo() call, %p\n", this);
23 }
24
25 public:
26 virtual void bar() {
27 printf("Object::bar() call, %p\n", this);
28 }
29 };
30
31 class Event : public Object
32 {
33 public:
34 Event() { printf("Event construct, %p\n", this); }
35 ~Event() { printf("Event deconstruct, %p\n", this); }
36
37 public:
38 void foo() {
39 printf("Event::foo() call, %p\n", this);
40 }
41
42 public:
43 virtual void bar() {
44 printf("Event::bar() call, %p\n", this);
45 }
46 };
测试代码
48 typedef void (Object::* OBJ_OFF)(void);
49 typedef void (Event::* EVENT_OFF)(void);
50
51 Object obj;
52 Event event;
53
54 int main(int argc, const char* argv[]) {
55 OBJ_OFF qf = &Object::foo;
56 (event.*qf)();
57
58 EVENT_OFF xf = &Event::foo;
59 (event.*xf)();
60
61 OBJ_OFF qf1 = &Object::bar;
62 (event.*qf1)();
63
64 EVENT_OFF xf2 = &Event::bar;
65 (event.*xf2)();
66
67 return 0;
68 }
输出
Object construct, 0x602120
Object construct, 0x602128
Event construct, 0x602128
Object::foo() call, 0x602128
Event::foo() call, 0x602128
Event::bar() call, 0x602128
Event::bar() call, 0x602128
Event deconstruct, 0x602128
Object deconstruct, 0x602128
Object deconstruct, 0x602120
网友评论