zhangleibo

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;

  

 

分类:

技术点:

相关文章:

  • 2021-12-05
  • 2021-04-19
  • 2021-12-09
  • 2021-05-30
  • 2021-12-15
  • 2021-08-06
  • 2021-08-06
  • 2021-12-15
猜你喜欢
  • 2021-08-06
  • 2021-08-06
  • 2021-08-06
  • 2021-12-05
  • 2021-10-12
  • 2021-12-27
  • 2021-12-15
相关资源
相似解决方案