I have a program that defines changes the target function of a function pointer from within the function being called by said pointer, like so:
void increment(int&);
void superincrement(int&);
void (*fooncrement)(int&);
int main() {
int j = 0;
fooncrement = increment;
while (true == true) {
fooncrement(j);
}
}
void increment(int& i) {
static int counter = 0;
i++;
if (counter > 7)
fooncrement = superincrement;
counter++;
}
void superincrement(int& i) {
i += 23;
}
A quick run through MSVC’s debugger shows that the program more or less works as expected. However, are there are any problems not immediately obvious here that might manifest if I tried something like this in a more complex environment?
This is well-defined.
In fact, this technique is often used to implement state machines.