20

Is it possible to convert strings into variables(and vise versa) by doing something like:

makeVariable("int", "count");

or

string fruit;
cin >> fruit;    // user inputs "apple"
makeVariable(fruit, "a green round object");

and then be able to just access it by doing something like:

cout << apple; //a green round object

Thanks in advance!

Rhexis
  • 2,414
  • 4
  • 28
  • 40
  • possible duplicate of [Access variable value using string representing variable's name in C++](http://stackoverflow.com/questions/2911442/access-variable-value-using-string-representing-variables-name-in-c) – outis Dec 27 '11 at 06:57

7 Answers7

20

No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.

If you know you're going to create a variable then create it right away:

int count;

What you might not know ahead of time is the variable's value, so you can defer that for runtime:

std::cin >> count;

If you know you're going to need a collection of variables but not precisely how many of them then create a map or a vector:

std::vector<int> counts;

Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possible nor useful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.

Community
  • 1
  • 1
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • 3
    @Flyphe -- it's absolutely possible to make a program that changes what it *does* based on the input; it's not possible to make a program that creates new names for data while the program runs. The key thing to realize is that those names -- variable names -- *only exist in the source code*, and have no function while the program is running anyway. Creating new ones would serve no purpose. Maps, vectors, arrays, lists, etc can hold arbitrary amounts of arbitrary data based on user input, but this has *nothing* to do with creating new variables. – Ernest Friedman-Hill Aug 22 '11 at 04:37
  • 1
    Of course it is. It's just that creating variables at runtime is not possible in C++. In C++ there is a clear distinction between code and data. Code is baked in, at compile time; data can be baked in or taken at runtime. Good C++ programmers will always try to do as much as possible at compile-time, because that means there is less work to do at runtime, which in turn means your program is faster. This is also why C++ programs have this speed reputation. We bake the variables in at compile-time, so all that's left to do at runtime is fill them in. – wilhelmtell Aug 22 '11 at 04:38
11

You can use a map.

map<string, int> numbers;
numbers["count"] = 6;
cout << numbers["count"];
sje397
  • 41,293
  • 8
  • 87
  • 103
6

Beginning programmers ask this question regarding every language. There are a group of computer languages for which the answer to this question is "yes". These are dynamic, interactive languages, like BASIC, Lisp, Ruby, Python. But think about it: Variable names only exist in code, for the convenience of the programmer. It only makes sense to define a new variable while the program runs if there's a person to then subsequently type the name of the variable in new code. This is true for interactive language environment, and not true for compiled languages like C++ or Java. In C++, by the time the program runs, and the imaginary new variable would be created, there's no one around to type code that would use that new variable.

What you really want instead is the ability to associate a name with an object at runtime, so that code -- not people -- can use that name to find the object. As other people have already pointed out, the map feature of C++'s standard library gives you that ability.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    yes but function for printing name and variable value could be useful while debugging, instead of that there is prepocessor which status is known http://www.cplusplus.com/forum/beginner/11252 – Qbik Mar 03 '13 at 14:12
5

You might want to look at C++ map.

Dair
  • 15,910
  • 9
  • 62
  • 107
1

At least for the (vice versa) there is a possibility with the preprocessor statement stringify #. See this answer on how to convert a C++ variable name into an string.

Community
  • 1
  • 1
math
  • 8,514
  • 10
  • 53
  • 61
1

No. C++ is statically typed, and this goes against that whole paradigm.

I have seen this type of functionality implemented before by storing variables in an stl map.

feathj
  • 3,019
  • 2
  • 22
  • 22
-4

well i guess u cannot make dynamics variables but u can use some function to write a new variable and its value in any external text file and access its value from that file where ever it is needed (u can also remove the dynamic variable by removing it from the text file.)

theory: variables are places in memory where we store data, identified by a name, we can store data in a text file if processor doesnot allow us to store it in registers, and we can access its value by searching its identity (name of variable) int the text file, our data will be next to it.

its just an idea, it should work but i guess it will be not very simple and ur program will have to pay in terms of speed.

Murtaza
  • 11