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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With