Naming VARIABLES?

Started by
15 comments, last by FxMazter 20 years, 8 months ago
Hi, I''ve been wondering about a thing for some while... In PHP you can name a variable by content of another variable: variable test = "variable1"; variable *test = 22; result: variable1 = 22; Now that is not the syntax... but i know u can do it something like that. So how would I name a variable by the content of a string with c++? Could you guys tell me what topics to look for?
Advertisement
Just curious... for what purpose?
I don''t think it is posible to name varibles like that in c++. Thats why we have arrays and dynamic memory alocation (combined with arrays or linked lists..). I don''t know why you would want to assign varible names during runtime since they are really only used at compile time. By that I mean the compiler doesn''t convert varible names to binary and puts them in the exe, but uses memory addresses in place of the names.

Correct me if I''m wrong of course

-J
quote:Original post by FxMazter
Hi, I''ve been wondering about a thing for some while...

In PHP you can name a variable by content of another variable:

variable test = "variable1";
variable *test = 22;

result:
variable1 = 22;

Now that is not the syntax... but i know u can do it something like that.

So how would I name a variable by the content of a string with c++?

Could you guys tell me what topics to look for?


Well if im understanding you correctly you need to look in to casting, although what youre asking is seemingly rather odd.

An ASCII tetris clone... | AsciiRis
An ASCII tetris clone... | AsciiRis
What that does look like though is pointer syntax. Your doing something that looks like this:

int variable1;int* test = &varible1*test = 22;  


the result of variable1 would be 22 using the * operator. Read in to pointers if your not too sure of what I just wrote. Sorry if you already know about them

-J

[edited by - jason2jason on August 15, 2003 4:47:25 PM]
I don''t know PHP so I could be missing the point, but anyway the short answer is: No.

Longer answer:
C++ is compiled into machine code. In machine code, there''s no such thing as variable names, only memory adresses. C++ has no way of knowing that variable1 used to be named "variable1".

Workaround:
This probably doesn''t help much, but the closests thing I can think of is using a map from strings to some other type.
map<string, int> myMap;string test = "variable1";myMap[test] = 22;
"-1 x -1 = +1 is stupid and evil."-- Gene Ray
Ok FxMazter now you really have to elaborate because we have all taken different meanings of what you said.

An ASCII tetris clone... | AsciiRis
An ASCII tetris clone... | AsciiRis
Well... I have looked into the PHP syntax and it is like this:

$variable = "Billy"
$$variable = 21

Result of printing $Billy:
21

Now the reason behind this is nothing special... but just out of curiosity.

Thank you for the posts anyway

[edited by - FxMazter on August 15, 2003 5:03:56 PM]
This is what we like to refer to as a SECURITY RISK!!!

imagine this situation:
$importantVariable="importantData";
$test="importantVariable";
$$test="stupidlyOverwritingImportantData";

Now, I know you would LIKE to think you would never do something like this, but it is possible.

I imagine that this could also be exploited by a malicious (and malodorous) user.

Do you use your powers for good or for awesome?
My newly updated site | The Cutter Project | Association of Computing Machinery

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Hehe... capn_midnight you are so right

[edited by - FxMazter on August 15, 2003 5:23:05 PM]

This topic is closed to new replies.

Advertisement