Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Variable Setting


ghost's Avatar
0 0

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


ghost's Avatar
0 0

No, I do not believe that is possible in a compiled language like C++. However, in PHP, an interpreted language, it is possible.


ghost's Avatar
0 0

Fair enough, thanks.

How is it done in PHP anyway? :)

Thanks


ghost's Avatar
0 0

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


mido's Avatar
Member
0 0

I can't entirely understand you, but didn't you try to use pointers?


ghost's Avatar
0 0

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]


ghost's Avatar
0 0

"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?


ghost's Avatar
0 0

lol I think you just answered your own question in the sentence after asking it, right?

and,

Skunkfoot wrote: Anyway, why do you want to do that?

See my previous post edit :)


mido's Avatar
Member
0 0

#include <iostream>

using namespace std;

int main() { int x = 1; int * y = &x; int V = * y;

cout << V;

return 0; }

If you built this, it will yield that V = 1, this is a basic idea, you can implement this on what you want, can't you?


ghost's Avatar
0 0

Possibly, although my problem is that if I set a variavle from another place, the variable name.

I don't know if that made sense.


mido's Avatar
Member
0 0

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.


ghost's Avatar
0 0

The simplest example of "another place" would proabaly be a file. Does that help? I'm sorry, I'm not too good at explaining myself am I…


ghost's Avatar
0 0

Just so we're clear, is this what you're trying to do?

Say you have a configuration file, like

something = asdf

And then you want to read the file and create a variable called "something" with the value of "asdf"?


ghost's Avatar
0 0

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.