vendredi 30 septembre 2022

use std::string to call a function in C++11

I want to use string value to call a function. Is this possible? Can someone please show some implementation. I would appreciate it.

class obj {
    int num1;
    int num2;
}

int func1(obj o) {
   return o.num1 + o.num2;
}

std::map<std::string, std::string> funcNameMap = ;

int someFunction(std::string funcName, obj o) {
    // Get the value associated with the string "funcName" i.e. "func1". 
    // Calls function "func1" with value "o" and returns the value.
}

int main(){
   obj o;
   o.num1 = 1;
   o.num2 = 2;
   auto x = someFunciton("func1", o);
   std::cout << x << std::endl;
   return 0;
}


Aucun commentaire:

Enregistrer un commentaire