如何创建一个函数指针作为参数(C++)? 我有这样的代码创建一个将函数指针作为参数的函数指针
#include <iostream>
using namespace std;
int kvadrat (int a)
{
return a*a;
}
int kub (int a)
{
return a*a*a;
}
void centralna (int a, int (*pokfunk) (int))
{
int rezultat=(*pokfunk) (a);
cout<<rezultat<<endl;
}
void main()
{
int a;
cout<<"unesite broj"<<endl;
cin>>a;
int (*pokfunk) (int) = 0;
if (a<10)
pokfunk=&kub;
if (a>=10)
pokfunk=&kvadrat;
void (*cent) (int, int*)=¢ralna; // here i have to create a pointer to the function "centralna" and call the function by its pointer
system ("pause");
}
你可能想考虑保留你的英文变量名。外国(你)的程序员会更容易理解你的代码。我不只是谈论讲英语的母语程序员。只是一个想法。 – 2010-07-07 22:55:38
是的,我知道,我已经翻译过它,但我偶然粘贴了这段代码,我刚刚注意到它。但回复很有帮助 – mbaam 2010-07-07 22:59:53