0

This might seem a bit far fetched and possible off-topic (sorry if it is), but I'd like to know for sure if it is possible or not.

I am working on a Q and A program.

The text file is laid out in a Question tab Answer newline style.

My question is this: Is it possible to read an answer as a function.

Example:

Question - What time is it? / Answer - getCurrentTime()

Question - What is today's date? / Answer - getCurrentDate()

Then the program, though string parsing, knows that this is a function without an argument and calls the function getCurrentTime() or getCurrentDate() which prints the time or date respectively.

CodeMonkey
  • 1,136
  • 16
  • 31
  • 2
    Use a `std::map` that maps strings to functions. – Barmar Sep 24 '14 at 16:40
  • Along the lines of what @Barmar said, there are questions on SO that explain how to do that. http://stackoverflow.com/questions/3113139/how-to-create-mapstring-classmethod-in-c-and-be-able-to-search-for-functi – Cory Kramer Sep 24 '14 at 16:41

1 Answers1

0

This is possible using an array of function pointers. You just load all the functions into the array. How you obtain the correct index is up to you. The only useful way I can come up with is maintain a second array containing the function names in the same positions as the functions in the function array. Then search the function name array and use the index in that array to access the correct function in the function array. If you need a better explanation leave a message. It is very late at night here and I need to work in the morning.

Barmar's solution will work to and is the better way to go about it but use function pointers.

Hope this helps dannyhut

dannyhut
  • 179
  • 11