In PHP, there is a feature:
$func_name = 'print' $func_name('test') Is it possible to do this in C ++?
In PHP, there is a feature:
$func_name = 'print' $func_name('test') Is it possible to do this in C ++?
Yes, there is such a thing in C ++, there are pointers to functions, see the code:
#include <iostream> using namespace std; int example(const int a, const int b){ return a * b; } typedef // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π½ΠΎΠ²ΡΠΉ ΠΏΡΠΎΡΠΎΡΠΈΠΏ (Π² Π΄Π°Π½Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΡΠ½ΠΊΡΠΈΡ) int // Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ (ΡΠ°ΠΊΠΎΠ΅ ΠΆΠ΅ ΠΊΠ°ΠΊ Π² ΡΡΠ½ΠΊΡΠΈΡΡ
) (*func) // ΠΈΠΌΡ ΠΏΡΠΎΡΠΎΡΠΈΠΏΠ° (Π² ΠΊΠΎΠ΄Π΅ ΡΠΏΠΎΡΡΠ΅Π±Π»ΡΠ΅ΡΡΡ Π±Π΅Π· Π·Π²Π΅Π·Π΄ΠΎΡΠΊΠΈ) (const int, const int); // ΡΠΏΠΈΡΠΎΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ² (ΡΠ°ΠΊΠΎΠ΅ ΠΆΠ΅ ΠΊΠ°ΠΊ Π² ΡΡΠ½ΠΊΡΠΈΡΡ
) int main(){ func test = example; cout << test(3, 2) << endl; return 0; } result:
typedef int (*FnPtr)(int, int); - this is a function pointer - perfectIf filename is NULL, then the returned handle is for the main program. (and in Windows (you probably are mostly interested in it?), I suspect, too, db. something like that) - avpHere is an example for Linux, how to search and call functions by name
(all details in man dlopen and SEE ALSO there)
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <dlfcn.h> #include <gnu/lib-names.h> // Defines LIBM_SO (which will be a string such as "libm.so.6") #ifdef __cplusplus extern "C" { #endif double drand () { return (double)rand(); } double drand2 (double a) { return ((double)rand() / RAND_MAX) * a; } #ifdef __cplusplus } #endif int main (int ac, char *av[]) { void *hmain = dlopen(0, RTLD_LAZY), *hmath = dlopen(LIBM_SO, RTLD_LAZY); if (!hmain) exit((fprintf(stderr, "dlopen(main): %s\n", dlerror()), 1)); if (!hmath) fprintf(stderr, "dlopen(%s): %s\n", LIBM_SO, dlerror()); char fname[101]; double a; int rc; while ((rc = scanf("%100s %lf", fname, &a)) != EOF) { if (rc == 2) { double (*f)(double) = (__typeof__(f))dlsym(hmain, fname); if (!f && hmath) f = (__typeof__(f))dlsym(hmath, fname); if (f) printf("%s(%f) = %f\n", fname, a, f(a)); else fprintf(stderr, "dlsym(%s): %s\n", fname, dlerror()); } else fputs("Invalid input, try again or finish by ^D\n", stderr); } return puts("End") == EOF; } Conditional compilation, including extern "C" { needed so that the names of functions when using a cross (C ++, g ++) compiler can be entered in the same way as you write them (and not something like _Z5drandv and _Z6drand2d ) .
Checked in gcc and g ++.
You need to compile with the -ldl flags (to connect the library directly with dlopen() , dlsym() , etc.) and -rdynamic so that the names of the functions defined in the module itself (here in the file with main() ) become available dlsym.
Those.gcc fncall.c -ldl -rdynamic -Wall
org++ fncall.c -ldl -rdynamic -Wall
#define . I showed that in C / C ++, as a system programming language, there are much more flexible means (real dynamics), about which the author m. and did not suspect (although, from his question, as it seemed to me, it should be just the opposite - he suspected that there was something like that, but did not know how to express it). - avpSource: https://ru.stackoverflow.com/questions/539148/
All Articles
switch(for example, a script) that will return a name to the function by name. - Vladimir Gamalyan