0

I was wondering if there is a way to change a certain variable's value if I know it's name?

For example,say you have a struct called 'profile' who has sveral variable in it: id, address, height, weight, and so on. Assume all variable have default values and the value of 'height' is 60. Now let say someone gave you a string as an input:"height=80". What I want to do is to be able to read to input, parse it and change the value to 'height' to 80. The first two parts I can do (read and parse), what I don't know is how the change the value.

Any advice? I would appreciate it if you could add a code sample.

2
  • 6
    Have you tried anything till now? you always have an option to override = sign always a savior in such case. Commented Jan 17, 2014 at 14:17
  • Perhaps a more experienced developer can confirm/deny, but I thought that the variable name is just a label for the coders. At runtime the program uses internal names so it won't be able to parse an input stream and match that with what you called it in the code. No? Commented Jan 17, 2014 at 14:23

2 Answers 2

3

You are asking for reflection. As far as I know C++ does not offer this feature or only very limited and maybe compile time only. Maybe this question might help: How can I add reflection to a C++ application?

Java and C# offers this feature, but a lot of people are skeptical about its use. In your case I would create a map containing the strings and pointers. You can simply use the string as the key and set it's value. If you have only a small number of parameter, just use a if-else branching.

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

Comments

0

C++ doesn't provide a mechanism to access a variable by name at runtime. The reason is that the compiled program doesn't use the names that appear in source code.

However, you could program some kind of lookup function. You would have to explicitly include all the variables you want to access though, which can be quite laborious. Here's a very simple example:

int g_width = 100;
int g_height = 50;

void setVariable(std::string name, int value)
{
    if (name == "width") g_width = value;
    else if (name == "height") g_height = value;
}

int main()
{
    setVariable("height", 80);
    return 0;
}

(I'm only using globals to keep it simple. I don't normally recommend them!)

You could extend the idea a lot. For example, you could make a more generic lookup table structure, which maps names to pointers (i.e. pointers to the actual variables in memory). Calling code would then be able to do whatever it wanted with the pointer, rather than relying on explicit functions like my setVariable() example above.

2 Comments

I tried to do it by map but with no success. Any advice? Would be glad to see some code sample
@user3206874: It's difficult to do a decent code sample in available space. A very basic idea would be to create a map like this: map<string, int*> vars;. You would add variables by name and pointer: vars["width"] = &g_width;. You would get variables out by pointer as well, e.g.: if (vars["width"] != 0) *(vars["width"]) = 60;. That obviously only deals with int variables. You would probably need polymorphism to handle multiple types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.