5

Possible Duplicate:
Convert string to variable name or variable type

How to use the string value as a variable name in c++

string listName = "hari";
string vectorName = "BF_vector_"+listName;
vector<string> vectorName;

vectorName.push_back("Some Value");

How to use the string value("BF_vector_hari") of vectorName as a variable name of vector.? Thanks in advance.

Community
  • 1
  • 1
DreamCodeer
  • 303
  • 2
  • 3
  • 13
  • You mean to dynamically make a vector variable that has the name "BF_vector_hari" similar to php's double $? –  Dec 27 '11 at 06:21
  • 6
    You ... don't! Even in languages that *do* support this ... think twice. –  Dec 27 '11 at 06:22
  • @EthanSteinberg yes. I want to give the variable name dynamically based on the string value. – DreamCodeer Dec 27 '11 at 06:22
  • 4
    No, you think you do, but you don't. There are better ways to solve your problem, whatever it may be. – Ed S. Dec 27 '11 at 06:24
  • All you guys are a bunch of complainers. While the termonology is bad("creating a dynamic variable"), the idea of wanting to store data with a string is a common design issue. –  Dec 27 '11 at 06:27
  • 4
    @EthanSteinberg: The terminology itself is the point. It bespeaks a fundamental misunderstanding of how C++ works. What he wants cannot be done *as he wants it*, specifically with dynamic *variable names*. You can create a mapping table, which is an object that maps names to *objects*, but it doesn't map them to *variables*. – Nicol Bolas Dec 27 '11 at 06:31
  • @EthanSteinberg: We're just saying in the comments in what you said in your (now deleted) answer; there are better ways to solve this problem. I would upvote you if I could. – Ed S. Dec 27 '11 at 06:40
  • @EthanSteinberg Thanks. I thought there might be some way for doing so. – DreamCodeer Dec 27 '11 at 06:48
  • possible duplicate of [Convert string to variable name or variable type](http://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type), [Access variable value using string representing variable's name in C++](http://stackoverflow.com/questions/2911442/) – outis Dec 27 '11 at 06:57
  • 3
    It's amazing how many people independently come up with the desire to do this horrible, nonsensical thing... it makes me wonder if there's some fundamental failure in our educational materials to explain what variables really are. – Karl Knechtel Dec 27 '11 at 07:10

2 Answers2

9

You can't in C++.

One thing you can do is use a form of std::map<std::string, std::vector> to store name to vector map.

Mat
  • 202,337
  • 40
  • 393
  • 406
6

You don't.

Variable names are a compile-time construct. The contents of a string are a run-time concept (string literals are slightly different, but those won't work either). Unless you write a specific mapping layer (which maps a string name to some object), you cannot just use a string as a variable name.

Or a type name for that matter.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982