I would like to code a utility function pointer inside my object that calls a group of other functions in the object. This utility function pointer should be initialized with default functionality, but be capable of override as well.
This is what I have so far:
struct GameState {
void(*setup)() = [](){};
void(*handleInput)() = [](){};
void(*update)() = [](){};
void(*draw)() = [](){};
void(*run)() = [this]() { handleInput(); update(); draw(); };
}
The compiler doesn't like the use ofthisin the capture of my lambda function and spits out the following errors all for that line:
E0413 - no suitable conversion function from "lambda []void ()->void" to "void (*)()" exists
C2440 'initializing': cannot convert from 'GameState::' to 'void (__cdecl *)(void)'
C2439 'GameState::run': member could not be initialized
std::function, however this will create a host of other problems, making your class uncopyable and/or unassignable. This is an XY problem. What is your real problem? No, not the one you're asking about, how to make function pointers work this way. But the real problem to which you believe the solution is to make function pointers work this way. This is the wrong solution. If you explain what your real problem is, I'm sure a real solution exists. - Sam Varshavchik