Can this be done? If so how?

Don't get hung up on why or what is global. This is the simplest example of the problem I am trying to solve.

Can I programmatically set aaa = 5 using the setvar() function?
-----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double aaa = 1.0;

void setvar(const char* 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?
}

int main(int argc, char **argv)
{
setvar("aaa",5.0);
// I want aaa = 5.0
printf("aaa = %lf\n",aaa);
}
Can I programmatically set aaa = 5 using the setvar() function?
Yes, that's the point of a global variable. You can change it from anywhere (where it is known).
Of course but that is not what I am asking? I am asking if setvar("aaa",5.0) can me made self aware of the char* "aaa", map that string to the real variable name aaa, and set it to 5.0?

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?
Also note I don't want a solution like:

if (!strcmp(var,"aaa"))
aaa = val;

Is there a general solution to mapping a string to a variable name? If so how?
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* TO DO find a better name */
struct Mapping 
{
   char name[25];
   double value;
};

Mapping mapping; // maybe can be array

void setvar(const char* 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?
  strcpy(mapping.name, var);
  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.
goes211 wrote:
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.
No, in the general case this can't be done, because C++ doesn't have reflection.
https://en.wikipedia.org/wiki/Reflection_(computer_programming)

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.
I had a feeling I was screwed. I was not thinking about what survives compilation but that makes sense.

Thx for the help.
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.
Last edited on
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.
Registered users can post here. Sign in or register to post.