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 ++, called a function pointer. - perfect
  • No, there is no call by dynamic name (that is, in fact, reflection) in C ++. But you shouldn't need this, there are other expressive means in C ++. - VladD
  • It is possible to make a switch (for example, a script) that will return a name to the function by name. - Vladimir Gamalyan

2 answers 2

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:

result

  • "func test = example;" - can example be a string? - Filipp Mustang
  • 2
    Here they write that there is not, but an option is shown how to get around. - Alekcvp
  • @Alekcvp and you look at the best answer there is used in the code typedef int (*FnPtr)(int, int); - this is a function pointer - perfect
  • @FilippMustang no, can not be a string for this case need parsing string (parsing) - perfect
  • one
    @FilippMustang, in certain cases, you can call and on the line without parsing ... Read man dlopen and pay attention to If 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) - avp

Here 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
or
g++ fncall.c -ldl -rdynamic -Wall

  • hello, the first thing that catches your eye is a whole bunch of code because of a banal task, if you look at the code of the question's author, all that happens there is just the creation of an alias for the native print function, isn't it easier to create such an alias using a duplicate pointer? - perfect
  • @perfect, perhaps for a specific task it is even easier to use the banal #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). - avp
  • yes, all right said such a task has no application. - perfect
  • @perfect, are you talking about calling a function by its name, received during the work of the program? - avp
  • No, I'm on the author's question. - perfect