需要注意:
1)函数指针所指向的函数,必须为全局函数或类的静态函数。
代码:
 #include "stdafx.h"
#include "stdafx.h"
 #include <iostream>
#include <iostream>

 class A
class A


 {
{
 typedef int (*fun)(void);
    typedef int (*fun)(void);

 public:
public:    
 void  Test(fun fun1)
     void  Test(fun fun1)

 
      {
{
 (*fun1)();
         (*fun1)();
 };
     };

 static int add(void)
    static int add(void)

 
     {
{
 std::cout<<"A::add()"<<std::endl;
        std::cout<<"A::add()"<<std::endl;
 return 2;
        return 2;        
 }
    }
 
    
 };
};

 int add2(void)
int add2(void)


 {
{
 std::cout<<"add2()"<<std::endl;
    std::cout<<"add2()"<<std::endl;
 return 2;
    return 2;
 
    
 }
}
 static int add3(void)
static int add3(void)

 
  {
{
 std::cout<<"add3()"<<std::endl;
     std::cout<<"add3()"<<std::endl;
 return 3;
     return 3;
 }
 }


 int _tmain(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[])


 {
{
 A a;
    A a;    
 a.Test(A::add);
    a.Test(A::add);
 a.Test(add2);
    a.Test(add2);
 a.Test(add3);
    a.Test(add3);
 return 0;
    return 0;
 }
}
 //output
//output
 //A::add()
//A::add()
 //add2()
//add2()
 //add3()
//add3()
 //Press any key to continue . . .
//Press any key to continue . . .成员函数指针:
 #include "stdafx.h"
#include "stdafx.h" 

 class CMemFuncPtr;
class CMemFuncPtr; 

 typedef int (CMemFuncPtr::*MemFuncPtr)(int, int);
typedef int (CMemFuncPtr::*MemFuncPtr)(int, int); 

 class CMemFuncPtr
class CMemFuncPtr 


 {
{ 
 public:
public: 
 int Add(int iFirst, int iSecond)
int Add(int iFirst, int iSecond) 


 {
{ 
 return iFirst + iSecond;
return iFirst + iSecond; 
 }
} 
 };
}; 

 int _tmain(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[]) 


 {
{ 
 MemFuncPtr pfnMemFunc = &CMemFuncPtr::Add;
MemFuncPtr pfnMemFunc = &CMemFuncPtr::Add; 
 CMemFuncPtr test;
CMemFuncPtr test; 
 (test.*pfnMemFunc)(1, 3);
(test.*pfnMemFunc)(1, 3); 

 return 0;
return 0; 
 }
} 

other samples:
 #include "stdafx.h"
#include "stdafx.h" 

 class Object;
class Object; 
 typedef int (Object::*MemFuncPtr)(int, int);
typedef int (Object::*MemFuncPtr)(int, int); 
 typedef int (*StaticMemFuncPtr)(int, int);
typedef int (*StaticMemFuncPtr)(int, int); 

 class Object
class Object 


 {
{ 
 public:
public: 
 // non-static member
// non-static member 
 int Add(int iFirst, int iSecond)
int Add(int iFirst, int iSecond) 


 {
{ 
 return iFirst + iSecond;
return iFirst + iSecond; 
 }
} 
 // static member
// static member 
 static int Sub(int iFirst, int iSecond)
static int Sub(int iFirst, int iSecond) 


 {
{ 
 return iFirst - iSecond;
return iFirst - iSecond; 
 }
} 

 //
// 
 typedef int (Object::*InClassMemFuncPtr)(int, int);
typedef int (Object::*InClassMemFuncPtr)(int, int); 
 typedef int (*InClassStaticMemFuncPtr)(int, int);
typedef int (*InClassStaticMemFuncPtr)(int, int); 
 };
}; 

 int _tmain(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[]) 


 {
{ 
 // Test non-static Add
// Test non-static Add 
 //
// 
 MemFuncPtr pfnMemFunc = &Object::Add;
MemFuncPtr pfnMemFunc = &Object::Add; 
 // Need to bind the member function to a instance (need a this pointer)
// Need to bind the member function to a instance (need a this pointer) 
 Object test;
Object test; 
 int res = (test.*pfnMemFunc)(1, 3);
int res = (test.*pfnMemFunc)(1, 3); 

 // function call missing argument list; use '&Object::Add' to create a pointer to member
// function call missing argument list; use '&Object::Add' to create a pointer to member 
 //MemFuncPtr pfnMemFunc1 = test.Add;
//MemFuncPtr pfnMemFunc1 = test.Add; 
 //res = (test.*pfnMemFunc1)(1, 3);
//res = (test.*pfnMemFunc1)(1, 3); 

 Object::InClassMemFuncPtr pfnMemFunc2 = &Object::Add;
Object::InClassMemFuncPtr pfnMemFunc2 = &Object::Add; 
 res = (test.*pfnMemFunc2)(1, 3);
res = (test.*pfnMemFunc2)(1, 3); 

 // Test Static Sub
// Test Static Sub 
 //
// 
 // cannot convert from 'int (__cdecl *)(int,int)' to 'MemFuncPtr'
// cannot convert from 'int (__cdecl *)(int,int)' to 'MemFuncPtr' 
 // Object::Sub Calling convention is __cdecl, but Object::*MemFuncPtr should be thisCall
// Object::Sub Calling convention is __cdecl, but Object::*MemFuncPtr should be thisCall 
 // function signature include calling convention, parameter list and return value..
// function signature include calling convention, parameter list and return value.. 

 //pfnMemFunc = Object::Sub;
//pfnMemFunc = Object::Sub; 
 //res = (test.*pfnMemFunc)(1, 3);
//res = (test.*pfnMemFunc)(1, 3); 

 StaticMemFuncPtr pfnStaticMemFunc = Object::Sub;
StaticMemFuncPtr pfnStaticMemFunc = Object::Sub; 
 res = (*pfnStaticMemFunc)(1, 3);
res = (*pfnStaticMemFunc)(1, 3); 

 StaticMemFuncPtr pfnStaticMemFunc1 = &Object::Sub;
StaticMemFuncPtr pfnStaticMemFunc1 = &Object::Sub; 
 res = (*pfnStaticMemFunc1)(1, 3);
res = (*pfnStaticMemFunc1)(1, 3); 

 Object::InClassStaticMemFuncPtr pfnStaticMemFunc2 = &Object::Sub;
Object::InClassStaticMemFuncPtr pfnStaticMemFunc2 = &Object::Sub; 
 res = (*pfnStaticMemFunc2)(1, 3);
res = (*pfnStaticMemFunc2)(1, 3); 


 // stl ? how to use this “function pointer”???
// stl ? how to use this “function pointer”??? 
 //std::mem_fun<int, Object>(&Object::Add);
//std::mem_fun<int, Object>(&Object::Add); 
 //std::mem_fun<int, Object>(&Object::Sub);
//std::mem_fun<int, Object>(&Object::Sub); 

 return 0;
return 0; 
 }
} 

总结:
1)一般的全局的函数指针,只可以使用静态,全局的函数。
2)类的成员函数指针,可以使用类的成员函数。可以使用类的public成员函数指针。