• Advertisement

Archived

This topic is now archived and is closed to further replies.

Naming VARIABLES?

This topic is 5367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement