Using Strings as variable names (again I know))

Hi all,

I am trying to use some strings to initialize variables, and I have read previous posts on the related issue, but I do not know how to utilize them on my problem.

I am not goo at explaining so I will just give a mock code of what I am want to do:
1
2
3
4
5
6
7
8
string firstArray [] = {"one", "two", "three", "four", "five"};
int arraySize = (sizeof(firstArray) / sizeof(string));

vector<string> firstVector;

for (int i = 0; i < arraySize; i++){
	firstVector.push_back(firstArray[i]);
} // So I now have a vector of those values which seems to be easier to deal with for me 


I would like to use a for loop of some sort to produce:
1
2
3
4
5
int one = 1;
int two = 2;
int three = 3;
int four = 4;
int five = 5;


And I don't quite understand how I can use the map function to do this, any ideas?

Cheers
Jason

Last edited on
some strings to initialize variables



You can't (well, not really at least. I suppose you could pass pointers to the variables and bleh, but...). You can use a map to create a collection of strings that are mapped to ints though.

http://www.cplusplus.com/reference/stl/map/

Why do you want to do this? What problem are your trying to solve that

1
2
3
4
5
int one = 1;
int two = 2;
int three = 3;
int four = 4;
int five = 5;


does not solve?
I have found examples in other languages like python through google which does that, but every c++ posts says no....

I am not only interested in using the int values, I am looking at initializing variables in customized classes and using the operators and constructors of those classes. So hard-coding a few thousand lines with the map method isn't really a solution for me.
other languages like python

It works in python because Python is an interpreted language. C++ is not. But depending on your concrete situation, there is probably a solution that actually does work in C++ you are not thinking about right now.
As a Python programmer and a C++ programmer, I am a bit confused by what your are after. Are you looking to inject a variable into a particular scope or namespace? Can you show us in Python code what you would like to do in C++?

C++ is a statically typed language. You cannot build up types on the fly like you can with a dynamically typed language such as Python. The trade-off here is in runtime performance. C++ does not have to lookup up method or variable names every time they are used, so it is tens to hundreds of times faster.
Hi,

I am also actually a beginner in Python as well, but I found this on a python forum
var myString = "rotationalSpeed";
$$myString = 4500;
echo $rotationalSpeed;

results in 4500.  This is called "variable variables" in PHP and it can
get hairy if done in a sloppy manner.


which is similar to what want. Referring to my first post, after I have created a vector<string> with its elements, I would like to use a for-loop to loop over each element, and use the string of that element as the name for the variable that I want to initialize. So I want to turn

string firstArray [] = {"one", "two", "three", "four", "five"};

into

1
2
3
4
5
int one;
int two;
int three;
int four;
int five;


by putting firstArray in a for-loop, so I won't have to hard code a few thousand lines to initialize the variables.

Hope I have expressed myself clearly enough by that, cheers again
but I found this on a python forum

You found PHP source code in a Python forum?

As to your question: That doesn't work. Languages like PHP and Python are interpreted on runtime, which is why you can create variables on runtime and access existing variables by their names. In C++, variable names only have a meaning on compile time, which makes this impossible to pull off in C++.
Last edited on
I can also tell you that you really do not want to do this. I can understand why a beginner would think it's cool, but its not. What you end up with is "overly dynamic" code which cannot be easily reasoned about or tested.

You really do want to use a map for this.

so I won't have to hard code a few thousand lines to initialize the variables.


You've got bigger problems if you need to initialize a few thousand variables inside your code. This is the problem you need to address. You cannot reason about or test code with more than a few variables in any one scope. What are you really trying to do?
I am unsure why so many people wanted to know exactly why I want to to this, as if this could work, it all starts with the elements in my initial array, which is something I hard-code in can won't go out of control. And I will need to get this working one way or the other to analyse my data.

In a very brief manner, I am wanting to do this because I need to analyse data (in .root format) using a program called ROOT, which uses the CINT interpreter, has has a very large customised C++ library (If I am using the terminologies right).

I need to analyse many different types of data independently, this will involve:

1. Initialize a TChain for each of them by their names independently;
2. Add the appropriate named .root files to the TChain using Add() within the TChain class;
3. Apply the same analysis to all of them;
4. Initialize histograms for each type and fill the surviving data respectively;

I have exaggerated a bit by calling it a few thousand lines, but definitely enough for anyone to think about looping it rather than hard-coding it, and that is unfortunately not something within my control.

Topic archived. No new replies allowed.