Skip to main content
gamedev.net
🔒 Locked

variable value to identifier

Started by BinaryZero 7 replies 300+ views
Original Post
BinaryZero
BinaryZero
i need to change the value of a vaiable into an identifier for example if i="hello", the value "hello" can somehow be used as an identifier name it is possible please someone hell me how
Chem0sh
Chem0sh
It''s very simple. You can''t (at least not there''s no way I know of).

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
felonius
felonius
Tell us what you are trying to do. There is very likely some other way to do it.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
BinaryZero
BinaryZero
the ## operator concatenates two identifiers for example if i said i##j it would return me ij as one variable identifier. if there was a way to use the value of a variable name as and identifier i could set a counter and tell it i##*counter which would return me i1, i2, i3... etc
BinaryZero
BinaryZero
by variable name i mean the value in the variable, sorry
dalleboy
dalleboy
You could use a string to object map.
std::map<std::string, int*> mapStringToInt;
mapStringToInt["hello"] = new int(47);
std::cout << *mapStringToInt["hello"];
EDIT: Darn HTML tags...

[edited by - dalleboy on June 9, 2002 6:39:21 PM]
Michalson
Michalson
quote:
Original post by BinaryZero
the ## operator concatenates two identifiers for example if i said i##j it would return me ij as one variable identifier. if there was a way to use the value of a variable name as and identifier i could set a counter and tell it i##*counter which would return me i1, i2, i3... etc


For that purpose why not use an array? I believe Visual Basic can do something like this, however be warned, no matter what language you do it in it will be very slow. When a compiler creates machine code it converts variable names into memory addresses/offsets (the names you give them are meanless to the computer and are discarded). By accessing a variable "by name" you would have to look up the name in some form of table (as dalleboy described) to get the correct address. This would be extremely slow and consume lots of memory (assuming every varible in your program could be accessed in this way).

Fruny
Fruny
quote:
Original post by dalleboy
EDIT: Darn HTML tags...



Use a [‍source] box, or stick &zwj;''s (zero-width joiner) in the appropriate places.

std::map::find() is more appropriate for a lookup than map::operator[]()

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
felonius
felonius
>> return me i1, i2, i3... etc

yes, or even just an array i[1], i[2], i[3] instead of i1, i2, i3.

[edited by - felonius on June 9, 2002 6:52:13 PM]
Jacob Marner, M.Sc.Console Programmer, Deadline Games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.