Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function by its name (std::string) in C++?

I wonder if there is a simple way to call a function from a string. I know a simple way, using 'if' and 'else'.

int function_1(int i, int j) {     return i*j; }  int function_2(int i, int j) {     return i/j; }  ... ... ...  int function_N(int i, int j) {     return i+j; }  int main(int argc, char* argv[]) {     int i = 4, j = 2;     string function = "function_2";     cout << callFunction(i, j, function) << endl;     return 0; } 

This is the basic approach

int callFunction(int i, int j, string function) {     if(function == "function_1") {         return function_1(i, j);     } else if(function == "function_2") {         return function_2(i, j);     } else if(...) {      } ...     ...     ...     ...     return  function_1(i, j); } 

Is there something simpler?

/* New Approach */ int callFunction(int i, int j, string function) {     /* I need something simple */     return function(i, j); } 
like image 589
Alan Valejo Avatar asked Oct 20 '13 02:10

Alan Valejo


People also ask

How can I call a function given its name as a string in C?

Put all the functions you want to select from into a dll, then use dlsym (or GetProcAddress on Windows, or whatever other API your system offers) to get the function pointer by name, and call using that.

How do you call a function by name as a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

Can you use std::string in C?

The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.


1 Answers

What you have described is called reflection and C++ doesn't support it. However you might come with some work-around, for example in this very concrete case you might use an std::map that would map names of functions (std::string objects) to function pointers, which in case of functions with the very same prototype could be easier than it might seem:

#include <iostream> #include <map>  int add(int i, int j) { return i+j; } int sub(int i, int j) { return i-j; }  typedef int (*FnPtr)(int, int);  int main() {     // initialization:     std::map<std::string, FnPtr> myMap;     myMap["add"] = add;     myMap["sub"] = sub;      // usage:     std::string s("add");     int res = myMap[s](2,3);     std::cout << res; } 

Note that myMap[s](2,3) retrieves the function pointer mapped to string s and invokes this function, passing 2 and 3 to it, making the output of this example to be 5

like image 94
LihO Avatar answered Sep 24 '22 01:09

LihO