Hi Heres an interesting problem that i came across recently..how to use string data to assign the name for a variable that i want to declare...
example:
Say i have,
char *str="VariableName";
I want a way to use the data from str to declare a variable,say,
int VariableName;
can it be done? if so how?
celu.
using a string data to create variable name?
Started by celu, Feb 24 2007 03:19 AM
8 replies to this topic
#1
Posted 24 February 2007 - 03:19 AM
#3
Posted 24 February 2007 - 04:53 AM
In C++, no, this isn't possible the way you described, because all variables have to be known at compile time. Many scripting languages allow things like this, for instance Javascript or PHP, since they are dynamic in nature. You can however create a data structure storing strings and integers like monjardin described.
reedbeta.com - developer blog, OpenGL demos, and other projects
#4
Posted 24 February 2007 - 07:36 PM
k..thx a lot for your inputs. I was just curious..I came across this problem a couple of weeks back..but then being not familiar with MAPS I couldnt think of a way to do it.So i chose to avoid the problem altogether and opted for a more straight forward alternative.this was something that kept nagging me,But now Its very reassuring to know that it actually cant be done :) (execpt thru using MAPS as monjardin had suggested .i.e.).
#5
Posted 24 February 2007 - 09:52 PM
Mostly when people want to do this is because of bad design. If you want to refer to an arbitrary variable, use something that is specifically designed for that: pointers. So the question is: why do you want to do this?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
#6
Posted 24 February 2007 - 11:17 PM
.oisyn said:
Mostly when people want to do this is because of bad design. If you want to refer to an arbitrary variable, use something that is specifically designed for that: pointers. So the question is: why do you want to do this? :)
What do you mean? Maybe I don't understand, I've never come across a mistake like this?
#7
Posted 25 February 2007 - 02:53 AM
I was talking about the topicstarter's intentions, not the others'
. Among beginning programmers (and I'm not saying that the topicstarter is a beginner, but I have no way of knowing that) I often seen it happen that they want to refer to variables programmatically like they do themselves. They do not realize that the name of a variable is just something to aid the programmer in writing down their program - as it would be very tedious to remember actual memory addresses.
So if they have variables with different names and they want to store a "reference" to one of those variables they would use a string, as that is a type that can hold a name to the variable. Of course, a string would do you no good as the variablenames do not exist in a compiled C++ program so there is no way of looking up the right variable at runtime so they get stuck. Of course, the right way to refer to a variable in this way is using a pointer (or reference) rather than a string. If this was the topicstarter's intent, using a map is definitely not the right way to go as that would only cause him to place all his regular variables in a map and refer to them by strings, rather than using the right tool for the right job.
Of course I wouldn't be so stupid as to state that you should never use an associated datastructure like a map, and perhaps this is in fact the right tool for the topicstarter's job. But I much rather hear his intent first before answering his question dead-on with possibly inferior solutions
So if they have variables with different names and they want to store a "reference" to one of those variables they would use a string, as that is a type that can hold a name to the variable. Of course, a string would do you no good as the variablenames do not exist in a compiled C++ program so there is no way of looking up the right variable at runtime so they get stuck. Of course, the right way to refer to a variable in this way is using a pointer (or reference) rather than a string. If this was the topicstarter's intent, using a map is definitely not the right way to go as that would only cause him to place all his regular variables in a map and refer to them by strings, rather than using the right tool for the right job.
Of course I wouldn't be so stupid as to state that you should never use an associated datastructure like a map, and perhaps this is in fact the right tool for the topicstarter's job. But I much rather hear his intent first before answering his question dead-on with possibly inferior solutions
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
#8
Posted 25 February 2007 - 03:11 PM
Hi Here where i got this Idea...In my directx application i had to read in an array of objects containing the light information from a maya Scene.Each object of the array contained information about the individual lights in the scene,which included a string that represented the light's name in maya.Now I had to create the corresponding DirectX lights in my application using the information from that array.I just thought it would be nice to give the Directx lights the same name as their corresponding names from maya.But when thought about it...I couldnt come up with a way to do this(and i now know the reason thx to Reedbeta..too bad it didnt strike me!)and so I resorted to using an array of directx light.But then this thought kept nagging me..and i wanted to know whether it could be done or not.Thats basically the reason behind my post.
Think i should have made it more clear in my first post :) ..anyway guess its never too late :)
Think i should have made it more clear in my first post :) ..anyway guess its never too late :)
#9
Posted 25 February 2007 - 03:38 PM
Gone through the same hassle for a little scripting language i'm doing for an engine. If really need those variable names create a class (vector one preferably) and in it create a string variable that'll hold the names of all the lights for you framework. Ofcourse you'll track by id-ing them with numbers but you could allow id-ing through string names (is more userfriendly than numbers...)... Good Luck ;^)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












