/* TO DO find a better name */
Mapping mapping; // maybe can be array
void setvar(constchar* var, double val)
printf("function that Sets %s = %lf\n",var,val);
// What can I do here that sets a real variable
// from a string with that variables name?
mapping.value = val
I could do something like that if I was creating the code from scratch. The problem is that code/data structures that I am working with already exist. I would like to find a solution to this generically. I am guessing it can't be done.
Imagine a command line arg that contains a variable name and a value. Can you generically map the command line variable name to a real variable and set it's value?
No. Names of variables (as well as names of most other source code entities) are not accessible to the program; they do not survive compilation. Eventually, standard C++ will get static reflection (making the program structure accessible at compile time), but for what you're describing, instead of a variable, you will need a data structure that associates a constant string with a mutable value.
The best that can be done is have an std::map<std::string, T *> that you use to obtain a pointer to the thing you want to set/get. The problem with this is that you have to explicitly add values to the map, and if you don't, you won't be able to set those values, even if they do exist in the program.
This is a problem with enums as well. You can't print the value of the enum's textual value, its 'lost' the same way. You can make a convoluted object that does all that, but its a memory hog, inefficient, and generally ugly -- lots of static strings wasting space (text is one of the most inefficient things in programs in terms of size to value).
and forget it if pointers or any complexity enter into it.
It's not really all that inefficient. If the enum is continuous the size of the lookup structure can be as small as (sum_of_all_strings + sizeof(void *) * enum_values_count).
And the strings are not wasting space. If you added reflection to that enum presumably its because you needed that feature for something.
In my experience, what I've seen is separate helper programs (such as a Windows Form) that then generate the mappings of class/variable name to the string it represents. The source code has a note that it was automatically generated by ____. Sort of similar to how a lot of GUI-creator programs will generate the underlying code for you.