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

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;
}

1

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

 

816

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.