How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.
The "right" way would be to make up some kind of persistent identification
system, like a name that (a) is unique (b) can be associated with a function
and (c) can be written to file and read from it. Such name would have the
association with a function in your code, and you can store the name and
then after reading it do the same thing every time.
A simple way to associate a name with a function is a map that has string
as its key and the function pointer as its value:
typedef std::map<std::string,void(*)()> FuncMap;
FuncMap myfunctionmap;
You will need to have some kind of function to populate the map (and that
is where the string-pointer association will be resolved every time your
program runs):
void populate_map()
{
myfunctionmap["func1"] = func; // 'func' is a function elsewhere
myfunctionmap["more"] = anotherfunc; // and so on
}
Then later if you need to store somthing to a file, store the string part
void write_to_file(void (*f)())
{
// search the map for a value that is the same as 'f', find its key
// store the key
}
and when you read the string from a file, you make the association through
your map:
void (* read_from_file())()
{
std::string s;
// get the string from the file
FuncMap::iterator it = myfunctionmap.find(s);
return (it == myfunctionmap.end()) ? NULL : (*it).second;
}
And so on.
I haven't made sure the code works, it's more to give you the idea of just
one way to do it.
Victor