0
点赞
收藏
分享

微信扫一扫

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


C++ 函数指针 函数名作为参数_#include


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



举报

相关推荐

C++函数作为参数

c语言--指针作为函数参数传递

函数指针——c++

0 条评论