I want to make a function that has one parameter (a string that contains the name of a variable in my program) and returns the value that the variable stores.
Asked
Active
Viewed 62 times
-2
-
2You'd need reflection here. That's a pretty big task. Can your variables be stored in a map? – Derek C. May 26 '20 at 20:28
-
1@Abhishek That question is about bash; this is about C++. – templatetypedef May 26 '20 at 20:31
-
Sorry, C++ doesn't work that way. – Jesper Juhl May 26 '20 at 20:33
-
https://stackoverflow.com/questions/2911442/access-variable-value-using-string-representing-variables-name-in-c – Abhishek Bhagate May 26 '20 at 20:33
1 Answers
4
Unfortunately, C++ doesn't provide a simple mechanism for reflection (the idea that the program can, at runtime, look up variable names by value or the like). You may need to consider using an alternate approach.
For example, you could make a std::unordered_map
whose keys represent certain values that you'd like to look up and whose values are populated with the items you'd like to look up.
Hope this helps!

templatetypedef
- 362,284
- 104
- 897
- 1,065