6 Replies - 304 Views - Last Post: 28 December 2011 - 10:35 PM Rate Topic: -----

#1 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Variable Name from string

Posted 28 December 2011 - 09:34 PM

I've yet to find a question quite so specific as this through a brief google search, and as this is more theory orientated it's of no consequence that I find specific code or not.

Anyhow, to the question: Is it possible to initialise a variable with the name of said variable being extracted from a string or other such storage methods.

Basically, can you create variables dynamically in C++ during runtime?

i.e.

char* String = "Blahblahblah";
int methodForTranslatingStringToVariable(String);

Thus resulting in an integer variable tagged as Blahblahblah to be created.

If not, what would one recommend as a pseudo-dynamic method; example being a variable tree where variables are only initialised when a string is passed containing the appropriate tag.

Regards, Benson.

Is This A Good Question/Topic? 0
  • +

Replies To: Variable Name from string

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Variable Name from string

Posted 28 December 2011 - 09:53 PM

None that I know of. Suppose you could: now how would you write code to manipulate such a variable if you don't know that it exists or will exist and you don't know its name?

If you describe what you're trying to accomplish in terms of actual functionality, maybe someone can suggest a way to implement it.

In other words, how were you planning to use these "dynamic variables"?
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Variable Name from string

Posted 28 December 2011 - 09:57 PM

You can't create on the fly. That said, typically someone who asks for this actually wants to create a mapping. In C++, you have std::map.
Was This Post Helpful? 0
  • +
  • -

#4 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Re: Variable Name from string

Posted 28 December 2011 - 09:58 PM

It's not so much implementation at this stage, it's more testing the boundaries of the language.

Define a variable from a string, read it via that string.

It's essentially using the string as a pointer, and allocating the memory for it to point at if it doesn't already exist. The difference being, I can basically have a separate IDE inside an existing program to handle debugging.

Excuse the unorthodox manner of the idea, but I live and breathe unorthodox ideas.
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Variable Name from string

Posted 28 December 2011 - 10:06 PM

You have to understand something about a variable. It's a code concept, rather than anything to do with your actual program. Computers have no notions of variables. In fact, if you inspect your generated optimised code, you'll see a lot of oddities. Functions won't exist, or will be changed. Variables will disappear, and so on.

So talking about changing variables from a program doesn't make sense, because that's something that doesn't exist unless you have code in front of you.
Was This Post Helpful? 1
  • +
  • -

#6 Bennos2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-July 11

Re: Variable Name from string

Posted 28 December 2011 - 10:16 PM

View PostOler1s, on 28 December 2011 - 10:06 PM, said:

You have to understand something about a variable. It's a code concept, rather than anything to do with your actual program. Computers have no notions of variables. In fact, if you inspect your generated optimised code, you'll see a lot of oddities. Functions won't exist, or will be changed. Variables will disappear, and so on.

So talking about changing variables from a program doesn't make sense, because that's something that doesn't exist unless you have code in front of you.


Fair enough. I've not delved too much into compiled code, so I will admit that that did not occur to me. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Variable Name from string

Posted 28 December 2011 - 10:35 PM

You like to see the power available in the compile time language of HLA ...
(See my links to HLA)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1