I would like the following simple function to call the function that called it, however the function is called by multiple functions, so it needs to recognize which function called it specifically and then call it.
int wrong()
{
std::cout << "WRONG \n";
return 0;
}
As a follow up, is this the sort of function that would be better expressed as a void?
What you want is a callback. Callbacks are implemented like this in C++:
typedef int (*CallbackType)( char c );
int wrong( CallbackType callback )
{
std::cout << "WRONG \n";
int r = callback( 'x' );
return r;
}
int also_wrong( char c )
{
return wrong( also_wrong );
}
Of course this will result in runaway recursion, so it will get you into a lot of trouble, but it definitely answers your question.
And yes, if all it does is to return 0
, then this is the sort of function that would be better expressed as returning void
.
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