Access Variable Value Using String Representing Variable's Name in C++

Access variable value using string representing variable's name in C++

As has been mentioned, you are looking for reflection in C++. It doesn't have that, and this answer explains why.

C program to get variable name as input and print value

In general, it is not possible to access at runtime global variables by name. Sometimes, it might depend upon the operating system, and how the compiler is invoked. I still assume you want to dereference a global variable, and you know its type.

Then on Linux and some other systems, you could use dlopen(3) with a NULL path (to get a handle for the executable), then use dlsym on the global variable name to get its address; you can then cast that void* pointer to a pointer of the appropriate type and dereference it. Notice that you need to know the type (or at least have a convention to encode the type of the variable in its name; C++ is doing that with name mangling). If you compiled and linked with debug information (i.e. with gcc -g) the type information is in its DWARF sections of your ELF executable, so there is some way to get it.

This works if you link your executable using -rdynamic and with -ldl

Another possibility might be to customize your recent GCC with your own MELT extension which would remember and later re-use some of the compiler internal representations (i.e. the GCC Tree-s related to global variables). Use MELT register_finish_decl_first function to register a handler on declarations. But this will require some work (in coding your MELT extension).



using preprocessor tricks

You could use (portable) preprocessor tricks to achieve your goals (accessing variable by name at runtime).

The simplest way might be to define and follow your own conventions. For example you could have your own globvar.def header file containing just lines like

 /* file globvar.def */
MY_GLOBAL_VARIABLE(globalint,int)
MY_GLOBAL_VARIABLE(globalint2,int)
MY_GLOBAL_VARIABLE(globalstr,char*)
#undef MY_GLOBAL_VARIABLE

And you adopt the convention that all global variables are in the above globvar.def file. Then you would #include "globvar.def" several times. For instance, in your global header, expand MY_GLOBAL_VARIABLE to some extern declaration:

 /* in yourheader.h */
#define MY_GLOBAL_VARIABLE(Nam,Typ) extern Typ Nam;
#include "globvar.def"

In your main.c you'll need a similar trick to declare your globals.

Elsewhere you might define a function to get integer variables by name:

 /* return the address of  global int variable or else NULL */
int* global_int_var_by_name (const char*name) {
#define MY_GLOBAL_VARIABLE(Nam,Typ) \
if (!strcmp(#Typ,"int") && !strcmp(name,#Nam)) return (int*)&Nam;
#include "globvar.def"
return NULL;
}

etc etc... I'm using stringification of macro arguments.

Such preprocessor tricks are purely standard C and would work with any C99 compliant compiler.

How to use input string, to call variable name in c++

Updated: Question misunderstood.


You cannot ref to a variable based on a string value.

But you can setup a list of structure who contain a combination of key and value. You should look at map

Access variable value where the name of variable is stored in a string

You provided the answer in your question. Try get.

> get(x)
[1] 1 2 3

How do you use a string or char to identify a variable by name in C++?

You don't.

Instead, you lay out your data differently, perhaps using a key-value store?

std::map<std::string, int> myData;
myData["cheese"] = 1337;

// ...

const std::string identifier = "cheese";
std::cout << myData[identifier] << '\n'; // 1337


Related Topics



Leave a reply



Submit