Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a function pointer from a function with certain parameters fixed in C++

Suppose I want to call a function f that asks for a function pointer of type

int (*foo)(int)

The function is something like

double f( int (*foo)(int))

I have a function that does this, but it takes another object.

int bar(int, MyClass*)

This object is created at runtime, say

int main()
    // ... read some file / input
    MyClass myclass = MyClass(input);

    // Now pass the function pointer
    double retval = f( bar( int, &myclass))

Is there any way to implement the last line? I am looking for an answer without global objects.

like image 794
Hans Avatar asked Feb 23 '23 06:02

Hans


1 Answers

No.

Do you have any influence over the function f? In C++, this would normally be either a template which would accept any callable object, or it would use a simple interface from which you could derive. In either case, you would define a class which could contain the additional data. Taking a pointer to a function, as such, is very poor design.

like image 182
James Kanze Avatar answered Apr 28 '23 14:04

James Kanze