You are here:

C++/Dynamic Variable Names


Is it possible to declare a variable with a name from a string.

IE - I call a function with a string and a value like so

MakeVariable("VariableA", 25.5);

and the function does something to this effect.

MakeVariable(string var, double val)
  // Where var is actually "VariableA" from
  // function call above.
  double var = val;


So now I would be able to use the double variable named "VariableA".

I don't think this is possible in C++, I know you can do it in PHP and I'm pretty sure you can do it in Python.

Let me know what you think.


Dear David,
Thanks for your question.
Well the main function worked connectly
but as soon as you enter the function, it
wont work, because the compiler will be
confusd in both of names and it will give a error like

"Multiple Declarations of Var"

So this kind of things doesnt work in C++, because
C++ is quiet commonly known as the Strongly typed
language, i guess you understand the term of Strong
typing. I hope this help you to some extent.

Best of Luck,


All Answers

Answers by Expert:

Ask Experts




I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.


I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2016 All rights reserved.