2

is it possible to execute a function stored as a string? e.g to execute a function in the form:

string str="myFunction()"; -> here i would like to execute "myFunction()"; 

Thank you!

3
  • 1
    C++ is not interpreted. Commented Mar 4, 2013 at 9:49
  • 4
    This is clearly an XY question, where you want to solve some problem X, and Y is how you see the solution, so you ask how to do Y. If you explain what the X is that you are solving, we'd probably be able to give a much better answer to how to solve the X problem. It may be that one of the answers below are correct, but it's also quite possible that you wouldn't want either of those, but something completely different... Commented Mar 4, 2013 at 9:57
  • what is the enviornment you are working .. linux or windows ? Commented Mar 4, 2013 at 10:04

7 Answers 7

2

You would have to compile it into a shared library, load that library, and call into it. It can be done, but it's not pretty. Most likely, there is a good way to do whatever is it you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

2

Apart from what David said, you could also create a mapping where each node contains the name of the function and a pointer to the function. Then look up the node by name in the mapping and call the function pointer. This assumes that all functions have the same prototype.

Comments

0

You can use the Qt library and QMetaObject::invokeMethod

QMetaObject::invokeMethod( pObject, "myFunction" ); 

Comments

0

You can do this when the function is exported form some DLL file.

void (*myFunction)(); HMODULE library = LoadLibrary("LIBRARY.DLL"); myFunction = GetProcAddress(library, "myFunction"); myFunction(); FreeLibrary(library); 

But this is not exactly what you want. Because you don't understand the internals of C++.

1 Comment

if you have list of functions and you have string and you want to call one fo the functions, you can do series of if-then-elsefi with strcmp's.
0

No, since C++ is a statically compiled language, this is not directly possible. Function and variable names are (normally) lost during compilation. Apart from using a shared library/DLL, as David suggested, you could also use a std::map and store your functions in it, e.g. (untested):

#include <functional> #include <iostream> #include <unordered_map> // or just <map> void myFunction() { std::cout << "in myFunction\n"; } void anotherFunction() { std::cout << "in anotherFunction\n"; } int main() { std::unordered_map<std::function<void()>> functions; // or just std::map // Store the functions functions["myFunction"] = &myFunction; functions["anotherFunction"] = &anotherFunction; // Call myFunction functions["myFunction"](); // Prints "in myFunction". } 

As, c.fogelklou already said, the functions must have a compatible prototype; Passing parameters via strings would even require writing a parser.

See also the documentation for std::unordered_map and std::function.

2 Comments

Thank you! Is your suggestion also valid if the function returns a value? e.g. an int? as I had difficulties with the above mapping solution if the function returns a value.
In the first line of main(), I declared the type of the function as std::function<void()>. If, e.g. your function returns int, you can substitute std::function<int()>.
0

You'd better try python, ruby or even matlab instead of c++.

But if you insist,you can try to integrate LUA in you project.

It can solve your problem.

Comments

0

You may also create and embed a language parser, that process and evaluate string you supply.

Further reading:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.