C++类成员函数指针

搜到一篇不错的文章,解释比较详细,转过来。没有验证其中代码的正确性,原文链接:http://blog.csdn.net/maojudong/article/details/8194143

测试目录:

1.普通函数指针指向普通函数
2.普通函数指向非静态成员函数
3. 类外部的 类函数指针 指向普通函数
4. 类外部的 类函数指针 指向成员函数
5. 类内部的 函数指针 指向成员函数 (类似于第2条)
6. 类内部的 函数指针 指向普通函数


直接上代码:
  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <map>  
  5. using namespace std;  
  6.    
  7. class Foo  
  8. {  
  9.     public:  
  10.         string m_str;  
  11.         Foo()  
  12.         {  
  13.             m_str = "";  
  14.         }  
  15.           
  16.         static void testFunc2(int a)  
  17.         {  
  18.             cout<<"Foo:void testFunc2(int a)"<<endl;  
  19.             cout<<a<<endl;  
  20.         }  
  21.           
  22.         void testFunc4(int a)  
  23.         {  
  24.             cout<<"Foo:void testFunc4(int a)"<<endl;  
  25.             cout<<a<<endl;  
  26.         }  
  27.         static void testFunc5(int a)  
  28.         {  
  29.             cout<<"Foo:void testFunc5(int a)"<<endl;  
  30.             cout<<a<<endl;  
  31.         }  
  32.           
  33.         void (*pTestFunc5)(int a);  
  34.         void (*pTestFunc6)(int a);  
  35. };  
  36.   
  37. void (*pTestFunc1)(int a);  
  38. void (*pTestFunc2)(int a);  
  39. void (Foo::*pTestFunc3)(int a);  
  40. void (Foo::*pTestFunc4)(int a);  
  41.   
  42. void testFunc1(int a)  
  43. {  
  44.     cout<<"func1 pointer test"<<endl;  
  45.     cout<<a<<endl;  
  46. }  
  47.   
  48. void testFunc3(int a)  
  49. {  
  50.     cout<<"func3 pointer test"<<endl;  
  51.     cout<<a<<endl;  
  52. }  
  53.   
  54. void testFunc6(int a)  
  55. {  
  56.     cout<<"func6 pointer test"<<endl;  
  57.     cout<<a<<endl;  
  58. }  
  59.   
  60.   
  61. int main(int argc, const char *argv[])  
  62. {  
  63.   
  64.     Foo foo;  
  65.     //foo.test("woo",100);  
  66.       
  67.     pTestFunc1 = testFunc1;     //经常用这个方法  
  68.     (*pTestFunc1)(1);  
  69.       
  70.      pTestFunc2=&foo.testFunc2;  
  71.     (*pTestFunc2)(2);  
  72.       
  73.     //pTestFunc3 = &testFunc3;  //编译器报错,不可以这么使用  
  74.       
  75.     pTestFunc4 = &Foo::testFunc4; //初始化的时候必须带有&Foo::  
  76.   
  77.     //pTestFunc4(4);//编译器报错,不可以这么使用  
  78.     //foo.pTestFunc4(4);//编译器报错,不可以这么使用  
  79.     //foo.*pTestFunc4(4);//编译器报错,不可以这么使用  
  80.     //foo.(*pTestFunc4)(4);//编译器报错,不可以这么使用  
  81.     (foo.*pTestFunc4)(4);   //正常运行  
  82.       
  83.     foo.pTestFunc5=&Foo::testFunc5;  
  84.     foo.pTestFunc5(5);  
  85.       
  86.     foo.pTestFunc6=&testFunc6;  
  87.     foo.pTestFunc6(6);  
  88.       
  89.     return 0;  
  90. }  

程序分析:
1.普通函数指针指向普通函数
pTestFunc = &testFunc;
或者
pTestFunc = testFunc;
调用方式
pTestFunc(1)
(pTestFunc)(1)
(*pTestFunc)(1)

2.普通函数指向非静态成员函数
pTestFunc=foo.testFunc2;  编译器报错,提示不匹配
error: argument of type ‘void (Foo::)(int)’ does not match ‘void (*)(int)’

pTestFunc=&foo.testFunc2;  编译器报错,提示不匹配
error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say ‘&Foo::testFunc’
error: cannot convert ‘void (Foo::*)(int)’ to ‘void (*)(int)’ in assignment

pTestFunc=Foo::testFunc2;  编译器报错
error: invalid use of non-static member function ‘void Foo::testFunc(int)’

pTestFunc=&Foo::testFunc2; 编译器报错
error: cannot convert ‘void (Foo::*)(int)’ to ‘void (*)(int)’ in assignment

普通函数指向静态成员函数
将代码更改一下后,将成员函数前加入一个static关键字
则下面的初始化方式编译和运行正常
pTestFunc2=Foo::testFunc2;
pTestFunc2=&Foo::testFunc2;
pTestFunc2=foo.testFunc2;
pTestFunc2=&foo.testFunc2;

调用方式和普通函数指向普通函数一致
pTestFunc2(2)
(pTestFunc)2(2)
(*pTestFunc)2(2)

3. 类外部的 类函数指针 指向普通函数
这种用法就是错误的,所以编译器不通过
pTestFunc3 = testFunc3;  编译器报错,
test5.cpp:83: error: cannot convert ‘void(int)’ to ‘void (Foo::*)(int)’ in assignmen
pTestFunc3 = &testFunc3;
test5.cpp:83: error: cannot convert ‘void (*)(int)’ to ‘void (Foo::*)(int)’ in assignmen

4. 类外部的 类函数指针 指向成员函数
初始化指针的方式
pTestFunc4 = &Foo::testFunc4; //初始化的时候必须带有&Foo::
pTestFunc4 = Foo::testFunc4; //编译器报错
pTestFunc4 = foo.testFunc4; //编译器报错
pTestFunc4 = &foo.testFunc4; //编译器报错

调用方式:
pTestFunc4(4);//编译器报错,不可以这么使用
foo.pTestFunc4(4);//编译器报错,不可以这么使用
foo.*pTestFunc4(4);//编译器报错,不可以这么使用
foo.(*pTestFunc4)(4);//编译器报错,不可以这么使用
(foo.*pTestFunc4)(4)//正常运行,所以必须要带有括号
如果foo为指针
Foo *foo=new Foo();
(foo->*pTestFunc4)(4)


5. 类内部的 函数指针 指向成员函数 (类似于第2条)
foo.pTestFunc5=foo.testFunc5;  编译器报错
test5.cpp:125: error: argument of type ‘void (Foo::)(int)’ does not match ‘void (*)(int)’

foo.pTestFunc5=&foo.testFunc5; 编译器报错
test5.cpp:123: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say ‘&Foo::testFunc’
test5.cpp:123: error: cannot convert ‘void (Foo::*)(int)’ to ‘void (*)(int)’ in assignment

foo.pTestFunc5=Foo::testFunc5;编译器报错
foo.pTestFunc5=&Foo::testFunc5;  编译器报错

声明为静态函数后(与第2条相似),编译和运行都OK
foo.pTestFunc5=foo.testFunc5;
foo.pTestFunc5=&foo.testFunc5;
foo.pTestFunc5=Foo::testFunc5;
foo.pTestFunc6=&Foo::testFunc5;

6. 类内部的 函数指针 指向普通函数
编译和运行都OK
foo.pTestFunc2=testFunc6;
foo.pTestFunc2=&testFunc6;


测试代码的下载位置:

注:

如果是在类成员函数内部使用函数指针,可以采用如下方法:

(this->*pTestFunction4)(4);


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值