Reputation: 3
How to create a function pointer which takes a function pointer as an argument (c++)??? i have this code
#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");
}
Upvotes: 0
Views: 840
Reputation:
you will find it easier to typedef function pointers.
typedef int (*PokFunc)(int);
typedef void (*CentralnaFunc)(int, PokFunc);
...
CentralnaFunc cf = ¢ralna;
Upvotes: 8
Reputation: 355009
You need to use the function pointer type as the type of the parameter:
void (*cent) (int, int (*)(int)) = ¢ralna
Upvotes: 3