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.
No. The variable names are just there to help you as a programmer make sense of the program. After the code has been compiled the variable names are of no importance.
In other words, the 'profile' should have a method, which takes input, and acts as necessary.
input := "height=80 foo=bar"
tokenize
=>
token0 := "height"
value0 := "80"
token1 := "foo"
value1 := "bar"
for each token:
IF "height" == token
THEN
IF value is an integer
THEN this->height := toInt(value)
ENDIF
ELSE IF ...
And yes, a map could be used to implement the IF .. ELSE IF ...
Boost has code to parse command line parameters into option variables.
Qt has "meta object" properties.
First, thanks for the help and for the code.
Second, from some reason, when I try to run your code I'm getting errors:
'personal:: id' : only static const integral data members can be initialized within a class
'Setter_type' : symbol cannot be used in a using declartion
//Etc
I managed to get rid of the first one by not initialzing it.
I couldn't get rid from the second. I tried to change it to:
Typedef std::function<void(persona*, std::string)> setter_type;
But now I get: error c2039: 'function' : is not a member of 'std'