C/C++中的成员函数指针声明及使用

代码:

 1 #include <iostream> 
 2 
 3 using namespace std; 
 4 
 5 class Test{
 6     public:
 7         void func(){
 8             cout<<"func"<<endl;
 9         };
10         static void stcFunc(){
11             cout<<"static func"<<endl;
12         };
13 };
14 
15 int main(int argc,char* argv[]){
16 
17     void (Test::*p1)();
18     p1 = &Test::func;
19     void (*p2)();
20     p2 = &Test::stcFunc;
21 
22     Test c;
23     (c.*p1)();
24     (*p2)();
25 
26     return 0;
27 }

输出:

func
static func

 分析:

注意普通成员函数与静态成员函数的不同。

posted @ 2016-05-15 14:05  hu983  阅读(357)  评论(0编辑  收藏  举报