函数指针定义

void func(string str){return;}

1.简单的函数指针的应用。
//形式1:返回类型(*函数名)(参数表)
void(*pFunc)(string);
 用法:pFunc = func;

    (*pFunc)("str");


2.使用typedef更直观更方便。
//形式2:typedef 返回类型(*新类型)(参数表)
typedef void(*pFunc)(string);

 用法:pFunc pfunc;

    pfunc = func;

    (*pFunc)("str");

3.在C++类中使用函数指针。
//形式3:typedef 返回类型(类名::*新类型)(参数表)

class B
{
public:
    void PrintStr(string val){
        cout << "class B " << val << endl;
    }
};

  用法: 

  typedef void (B::*pFunc)(string);

  pFunc ptrFunc;

  ptrFunc = &B::PrintStr;

  //1

   B b;

   (b.*ptrFunc)("b.*ptrFunc");

  //2

     B* pB = new B;
     (pB->*ptrFunc)("new B->ptrFunc");
     delete pB;

  

原文地址:https://www.cnblogs.com/zhangleibo/p/5790967.html