• Advertisement

Archived

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

Naming VARIABLES?

This topic is 5273 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
Hehe... capn_midnight you are so right

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

Share this post


Link to post
Share on other sites

quote:
Original post by FxMazter
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



Well i have no idea about PHP and i have absolutley no idea why there would ever be reason to assign a variable to a string, what youre suggesting is very unstable in C++, but you could use atoi() to cast the string to an int, or itoa() to do it the other way around.


An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on August 15, 2003 5:25:15 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany Smith
Well i have no idea about PHP and i have absolutley no idea why there would ever be reason to assign a variable to a string, what youre suggesting is very unstable in C++, but you could use atoi() to cast the string to an int, or itoa() to do it the other way around.

You don't understand what he's talking about. What the OP wants is absolutely impossible in C++. There's no way to do that (besides simulating "variables" with a table/dictionary/what have you).

Just because it's impossible in C++ doesn't mean that it can't be useful, though.

[edited by - twix on August 15, 2003 6:01:23 PM]

Share this post


Link to post
Share on other sites
What you''re asking seems awfully like something that pointers/references would be able to do. So I''m thinking along the same lines as Jason suggested over here:

quote:
Original post by Jason2Jason
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



So why doesn''t it answer your question?





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
So why doesn't it answer your question?


An analogous feature in Lisp is the function intern, which converts a string to a usable symbol (variable name). Maybe this will be a bit more illuminating than PHP, maybe not.

>> (setf name "VARIABLE")
"VARIABLE"
>> (set (intern name) 123)
123
>> name
"VARIABLE"
>> VARIABLE
123

See what that does? It's not like pointers or references.

...

Of course, in Lisp this "feature" is intensely pointless.

[edited by - twix on August 15, 2003 7:06:59 PM]

Share this post


Link to post
Share on other sites
Sounds like mappings to me. See the STL container "map".
The variable name becomes the key to the map, the value of the variable is the result of querying the key.

Share this post


Link to post
Share on other sites

  • Advertisement