C++ Get a function pointer or eval a function using the functions name stored in a string
I am working on a 开发者_C百科parser for natural language. Words from natural language are parsed to the concepts that they represent. The purpose is to allow a robot to interpret commands given in natural language. One way to execute these commands would be by creating an enormous amount of if statements, which would turn into at east one for each concept. I was wondering if there was a way to retrieve a function pointer to a function whose name is stored as string. Each concept could then contain a string that represented the function that would need to executed if the concept is present in the parse. This way the concept would already know what to do.
This is not so complicated. I would try to (theoretically, of course) implement one of these "possible" solutions:
This is very C++: Read Instantiate class from name? and create a class for each of the "natural" language, make sure that these classes derive from a common interface, and each of the classes would have the overridden method would handle the specific word.
This is more C: In case you are not afraid to use shared libraries, create a shared library, with all the functions for the commands you want, make sure the functions name match the "words" and then just load the specific function (word) you want to use. This is even easier than the one above, since you don't need to know all the words, and there is less maintenance.
Cheers
精彩评论