I have a GUI class which stores pointers to functions which are triggered on events such as a mouse down event. I would love to be able to read and write the chunk of memory which that function is stored in out a file and later load it back into memory like a DLL. Is there anyway to do this? Also I am fully aware of script interpreters such as Lua or Python but I want to actually write out a C++ function.
Share
There’s no practical way to do this. The problem is that parts of the function may be optimized away or inlined or split into pieces by the compiler. Furthermore, you’ll likely have issues with linking, calling conventions, and the like. Finally, the function might depend on global state in which case it won’t work anyway.
You’re best bet is to define a serialization format that represents a sequence of calls to a particular set of core functions in your applicaton. Or just use a scripting language, which is the same thing except better and done for you. Lua is very light weight to embed.