C++ Variable Setting
I'm trying to make it so I can set a variable which gets its name from the value of antoher variable.. is this possible in C++?
For example,
If I had a string variable: "str1" which held the value "name1"
markupstring str1 = "name1";
and I want to name the next variable "name1", or whatever else the balue of str1 may be.
Is this possible?
Thanks in advance
I'm trying to make it so I can set a variable which gets its name from the value of antoher variable.. is this possible in C++?
No, because the whole reason we have variables is because they change. If you were able to do that, the name of your second variable would be changing and you'd end up getting a bunch of "can't find this variable" syntax errors.
Also, I think your first variable should be a constant, but I still don't know how you'd do that…you have to define the variable name and data type regardless of what any other variable equals :P
Skunkfoot wrote:
No, because the whole reason we have variables is because they change. If you were able to do that, the name of your second variable would be changing and you'd end up getting a bunch of "can't find this variable" syntax errors.
Also, I think your first variable should be a constant, but I still don't know how you'd do that…you have to define the variable name and data type regardless of what any other variable equals :P
Yes, they do change… but, "If you were able to do that, the name of your second variable would be changing.." isn't quite true. I'd say you'd be right about the constant bit though.
I think I might be able to overcome my C++ problem using arrays. Maybe by using an array to list the "names" that I would've named the variables with, and have a a vector value. The integer of the index of the "name" in the array, would be the number it would be in the vector. You think that'd work?
[Edit (after readings Mido's post) ] An example of what I'm doing, is getting the name of a variable from somewhere and then it's value from the somewhere. Similar to a config file really, but not just when reading a file. Although, now I've brought it up, if anyone has any easy libraries I could use to read a ini file I would appreciate it :) [/Edit]
"If you were able to do that, the name of your second variable would be changing.." isn't quite true.
What's not true about that? If the name of your second variable, variable B, depends on the value of your first variable, variable A, and the value of variable A changes, then isn't it true that the name of B would also change? (although I guess not necessarily, maybe you just reinitialize the variable or something :P)
Anyway, why do you want to do that?
You make me understand you less than first… But when I saw "different place" I thought of classes. Make it more clear, so I can help you more.
In .Net languages, it would be a dictionary. In Python, I believe it's a tuple. In C++, though… Well, a multi-dimensional array would work, I guess; you could treat one column as the variable name and the other as the variable value. Might be some nasty type conversions in there, though. I could mention structs and enums, too, but I'm pretty sure those won't help you. That being said, I think that the bit about "classes" is probably the most relevant. That is, unless my bit about the multi-dimensional array works for you.