简体   繁体   中英

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

you will find it easier to typedef function pointers.

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

您需要使用函数指针类型作为参数的类型:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM