0

Is there a way to take a string as an input argument to a c++ function and evaluate it as an internal argument e.g. the name of a structure or other variable?

For example (written in pseudo code)

int myFunction(string nameStructure){ nameStructure.field = 1234 }

The "take away" point is converting the input string as a variable within the code.

Mark

Mark
  • 1,277
  • 3
  • 13
  • 27
  • Take a look at this answer: http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application .. what you're referring to is known as "reflection" and unfortunately in C++ is not 'possible' (you might be able to pull it off with some compiler hacks/tricks but no guarantee of portability) .. there are ways "around" it, but it's not something directly supported .. not like [RTTI](https://en.wikipedia.org/wiki/Run-time_type_information) for instance – txtechhelp Apr 25 '15 at 16:56
  • Take a look at the [Factory Design Pattern](http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns) which is used to create objects based on an input. – Thomas Matthews Apr 25 '15 at 18:27

3 Answers3

5

This type of question is often a symptom of a XY problem so consider other options first. That being said, there's no such default mechanism in C++ but there is a simple workaround I can think of - use a dictionary (std::map / std::unordered_map) to store all your objects:

std::map<std::string, MyAwesomeObject> objects;
...
int myFunction(std::string nameStructure)
{
    objects[nameStructure].field = 1234
}
Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
1

The names of local variables are just artifacts of the human-readable code and have no meaning in the compiled binary. Your int myIntVar's and char* myCharP's get turned into instructions like "four bytes starting at the location of the base pointer minus eight bytes, interpreted as a four-byte integer". They no longer have names as such.

alcedine
  • 909
  • 5
  • 18
-1

If you export symbols from your binary, you can at runtime to look into export table according to your binary format and find the variable you want. But i bet you want something like access to local variable and that is not possible.

If you really need this funcionality, take a look at more dynamic interpreted languages as php

http://php.net/manual/en/language.variables.variable.php

Krab
  • 6,526
  • 6
  • 41
  • 78
  • the user is asking about C++ and you refer them to PHP? – txtechhelp Apr 25 '15 at 16:50
  • txtechhelp: i am just giving advice that more dynamic and interpreted languages can have this functionality. What is the problem? – Krab Apr 25 '15 at 16:52
  • The user asked about C++ reflection (which has been [answered else where](http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application)) and you tell them to check out PHP instead; while C++ reflection isn't necessarily possible/portable, there are other ways around it (templates, RTTI, OO, etc.) that don't require the user possibly rewriting their entire application into another language .. instead you might want to suggest alternative methods in C++ to possibly achieve what the user wants (then suggest another language like Java/PHP if the user is not satisfied). – txtechhelp Apr 25 '15 at 17:00