mbaam
mbaam

Reputation: 3

Create a function pointer which takes a function pointer as an argument

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*)=&centralna; // here i have to create a pointer to the function "centralna" and call the function by its pointer

    system ("pause");
}

Upvotes: 0

Views: 840

Answers (3)

user322610
user322610

Reputation:

you will find it easier to typedef function pointers.

typedef int (*PokFunc)(int);
typedef void (*CentralnaFunc)(int, PokFunc);
...
CentralnaFunc cf = &centralna;

Upvotes: 8

MSN
MSN

Reputation: 54554

void (*cent)(int,int (*) (int))=&centralna;

Upvotes: 2

James McNellis
James McNellis

Reputation: 355009

You need to use the function pointer type as the type of the parameter:

void (*cent) (int, int (*)(int)) = &centralna

Upvotes: 3

Related Questions