歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++ 函數指針 函數名作為參數

C++ 函數指針 函數名作為參數

日期:2017/3/1 9:47:07   编辑:Linux編程

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

Copyright © Linux教程網 All Rights Reserved