-1
std::string VariableName = "name";
int (VariableNameHere) = 5;

From my understanding of c++ what I am asking is most likely impossible. If it is please post possible alternative solutions. Thanks!

RizzutoHD
  • 3
  • 2
  • 1
    It's possible with macros but the name still must be known at compile time. – 0x5453 Jan 08 '18 at 22:34
  • 5
    You could use a `std::unordered_map` and then do `mymap[VariableName] = 5;` – Gillespie Jan 08 '18 at 22:36
  • 2
    Alternatively `#define VariableName name` and then `int VariableName = 5;`, which relies on the preprocessor. But, since I abhor the preprocessor, please don't do that. – Eljay Jan 08 '18 at 22:45
  • Solutions to *what?* What are you actually trying to accomplish with this approach? Why do you need a variable whose name is defined in another variable? – Beta Jan 09 '18 at 00:04

1 Answers1

4

As you have it is not possible, you would need to have some kind of look-up system, such as:

std::map<std::string, int> variables;
...
variables["name"] = 5;
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Using regular data structures rather than variables with special, *magical* names is always better. – tadman Jan 08 '18 at 23:06