C++ 函数指针 函数名作为参数

来源:互联网 发布:云计算开发招聘 编辑:程序博客网 时间:2024/04/27 21:21

1.函数指针声明


typedef 返回类型(*函数指针类型名)(函参列表);

例子:

typedef int (*pf)(const int& a,const int& b);


2.函数指针例子


// MethodPoint.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;typedef int (*pf)(const int& a,const int& b);int sum(const int& a,const int& b){return a + b;}int minus(const int& a,const int& b){return a - b;}int doSometing(const int& a,const int& b, pf p){return p(a, b);}int _tmain(int argc, _TCHAR* argv[]){cout << doSometing(1, 2, &sum) << endl;cout << doSometing(1, 2, &minus) << endl;system("pause");return 0;}


http://www.waitingfy.com/?p=816



原创粉丝点击