D_E
D_E

Reputation: 1196

How to pass into the function pointer to function?

Is it possible to create a function which takes a pointer to another function? How does the prototype of such a function look like?

Upvotes: 4

Views: 394

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272507

typedef int (*func)(float, char);

int something_that_takes_a_func(func f) { return f(3.14, 3); }

int foo(float a, char b) { return a - b; }

std::cout << something_that_takes_a_func(&foo) << "\n";

Upvotes: 6

gunter
gunter

Reputation: 93

void f(int(*Func)())
{
    int a = Func();
}

and for a member function:

void f(int(cLass::*Func)())
{
    cLass *c = new cLass;
    int a = (c->*Func)();
}

Upvotes: 3

Related Questions