tags:

views:

356

answers:

7

How can I call a C++ function from a string?

Instead of doing this, call the method straight from string:

void callfunction(const char* callthis, int []params)
{
  if (callthis == "callA"
  {
    callA();
  }
  else if (callthis == "callB")
  {
    callB(params[0], params[1]);
  }
  else if (callthis == "callC")
  {
    callC(params[0]);
  }
}

In C# we'd use typeof() and then get the method info and call from there... anything we can use in C++? ~ Thanks!

+1  A: 

Alternative: You could use array of function pointers and call the desired function using its index.

typedef void (*TFunc)(int, int);

TFunc arrptr[100];

void callFunction(int index, int params[])
{
    (*arrptr[index])(params[0], params[1]);
}
fushar
+5  A: 

Create a std::map made of strings and function pointers. Create the map with all of the functions that you will want to call.

There are other ways to do it, involving symbol tables and dynamic loaders but those ways are not portable or friendly.

Zan Lynx
+3  A: 

Other solutions are variations on the same theme:

switch (callthis)
{
case FUNCA:  callA();    break;
case FUNCB:  callB(params);  break;
... etc.
}

Or search an array of stuctures:

struct {
    char *name;
    TFunc  f;
} funcdefs [] = {
    {"callA", callA},
    {"callB", callB},
    {"callC", callC},
    ... etc.
    {NULL, NULL}
};
for (int j = 0;  funcdefs [j] .name;  ++j)
    if (!strcmp (funcdefs [j] .name, callthis))
    {
         funcdefs [j] .f (params);
         break;
    }
wallyk
+2  A: 

May not be an option, but if you can use managed c++ (C++/CLI), you can do this just like you can in C#. This will require .NET though...

NotDan
+2  A: 

There is no good way of automatically doing what you ask. I'd consider two different ways, depending on how many functions you think will need to be called:

If there are only a few functions, stick with the code you have (note that if you wish to use const char*, you can't compare these strings with the == operator. You can use "strcmp()", by doing an "#include < cstring>").

If there will be many functions, or if you will be adding and removing functions from your list often, then you might want to use "std::map". This will map the function name string to a function pointer. I would probably wrap this in a class for ease-of-use:

class Str2Fun {
  std::map<std::string, (void*)(int**)> data;
 public:
  void add( const char *funName, (void*)(int**) funPtr );
  void call( const char *funName );
};
Colin
+2  A: 

You could also look at the answers to How can I add reflection to a C++ application? for information about RTTI, the reflection mechanism in C++.

Good luck.

Chip Uni
A: 

You could wrap your function declarations into an interpreted language (like Lua or Perl or Python (boost has some nice framework for that) ). Then use that language to call your code 'by string'.

These languages/wrappers are built to do such things. C++ isn't, so you will put a lot of effort in adding support for it.

xtofl