views:

90

answers:

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

    system ("pause");
}
+3  A: 

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

void (*cent) (int, int (*)(int)) = &centralna
James McNellis
ARGH! 1 minute apart!
MSN
thank you, now it works and i understand it
mbaam
+2  A: 

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

MSN
+8  A: 

Hi, you will find it easier to typedef function pointers.

typedef int (*PokFunc)(int);
typedef void (*CentralnaFunc)(int, PokFunc);
...
CentralnaFunc cf = &centralna;
Steven Jackson
this is very useful, but im not yet alowed to use it
mbaam
You should accept the best answer, not the answer that meets some unspecified requirements. You're welcome anyway.
Steven Jackson
@Steven: You're forgetting "best" may be defined to include "complies to unspecified requirements".
GMan
@GMan: You've spent enough time on here to know what the purpose of the answer feature is, but thanks for your input.
Steven Jackson
@Steven: Yes, it's the answer the *questioner* finds most helpful. And "helpful" may include anything the questioner wants, including compliance to certain specifications, made public or not. I agree `typedef`'s are the best way to go (I always tell others to use more `typedef`'s), but this answer is not helpful *to the questioner*. No biggie, you'll still get plenty of up-votes by people who agree with the answer, like me.
GMan