is it possible to call a function by name ina string?

 

I need to call a function by name. I will construct the function name in a string. once the string is constructed i need to call that function. something similar to how iCustom() function uses indicator's name in a string.

 

You can simply create a main function that receices the string and make it call the subfunctions accordingly, like

void function_by_string(string function_name)
  {
   if (function_name=="function_a"){function_a();return;}
   if (function_name=="function_xyz"){function_xyz();return;}
   // etc...
  
  }
 
abbasakh: I will construct the function name in a string. once the string is constructed i need to call that function.

I suggest you do not do that. There are infinite number of strings — what will you do with @Chris70 answer if you pass an invalid name? Can't be one with an enum.

enum FBN {A, XYZ};
void function_by_name(FBN function_name)
  {
   switch(function_name){
   case A:   function_a();  return;
   case XYZ: function_xyz();return; // or break.
   // etc...
   } // switch
  }
 

@William: I have examples in my own codes where I also did exactly that, i.e. with the switch operator, and at first I thought about suggesting the same.

I admit that in some cases using enumeration elements has some advantages, e.g. if you want to chose a function as a not hard-coded option selected through a user input variable.

The argument with the infinite possible number of strings - although it's formally correct - doesn't hold though as an improvement, because the number of elements in the enumeration also is predefined and finite (adding the "default" case essentially can't change that).

Plus: he specifically asked for a function name as a string variable, not an enumeration element, so you're answering a different question.

Reason: