I wanted to dynamically call a function by its name , e.g , suppose have the following function and string:
void do_fork()
{
printf ("Fork called.\n");
}
char *pFunc = "do_fork";
Now i need to call do_fork() just by *pFunc. So is that possible ?
Either C/C++ code is welcomed , many thanks !
Neither C nor C++ have enough reflection to do this out of the box, so you will have to implement your own scheme.
In C++, the more or less canonical way to do that is using a map of strings to function pointers. Something like this:
Of course, this is limited to functions with exactly the same signature. However, this limitation can be somewhat lifted (to encompass reasonably compatible signatures) by using
std::functionandstd::bindinstead of a plain function pointer.