函数指针

一. 函数指针的定义:

  函数指针是指向函数的指针变量。 重点: 1. "函数指针" 是个变量, 变量类型是指针  2.指向函数

二. 声明方法:

  返回值类 (* 函数指针变量名)(参数列表)

  例如:

  int func(int x);        //函数的声明

  int (*pFunc)(int x);    //函数指针的声明

  pFunc p = func;    //将func的首地址,赋值给函数指针p

  p = &func;

二. 调用示例:

#include <iostream>
using namespace std;


int add(int a, int b);    //函数声明
 
struct p_fun {
    int (*pFunAdd) (int a, int b);  //函数指针定义
};
 
int main() {
    p_fun pFun;
    pFun.pFunAdd = add;

 cout<<"the result is :"<< pFun.pFunAdd(2,4);
    return 0;
}

 
int add(int a, int b) {   //函数定义
    return a + b;
}

函数指针一般应用在回调函数和多线程中

https://blog.csdn.net/afei__/article/details/80549202

原文地址:https://www.cnblogs.com/darwen/p/12251463.html