函数指针

指向函数的指针

#include <iostream>
using namespace std;

int add(int a,int b){
    return a+b;
}

int reduce(int a, int b){
    return a-b;
}

int test(int a,int b, int (*pFunc)(int ,int )){
    return (*pFunc)(a,b);
}

int main(int argc, char const *argv[])
{
    int a,b;
    /**********用法一:函数指针作为参数*****************/
    cout<<"请输入2个整数(a和b):";
    cin>>a>>b;
    cout<<"a+b="<<test(a,b,add)<<endl;
    cout<<"a-b="<<test(a,b,reduce)<<endl;
    return 0;
}
iMac-52:a apple$ ./a.out 
请输入2个整数(a和b):1 3
a+b=4
a-b=-2

由此可见,函数指针可以解决多肽的问题(解析不同扩展名的excel)

函数指针的定义:

返回类型 (*变量名)(参数照抄);

results matching ""

    No results matching ""