Is it valid to change a pointer to a function (fun
here) from a function which was pointed to and during execution (foo
)?
using fun_p = void (*) ();
void foo ();
void boo ();
fun_p fun = &foo;
void foo () {
std::cout << "foo\n";
fun = &boo;
}
void boo () {
std::cout << "boo\n";
}
int main () {
(*fun) ();
(*fun) ();
return 0;
}
The reason why I want to do this is to run either foo()
or boo()
, starting with foo()
. Once condition is met in foo()
I want to change it once, and since next iteration (the code would be run in a loop of course), boo()
is invoked.