How to Use a String as a Variable Name in C++

C - Convert string to variable name

This is simply not possible in C.

Check out this question for more details.
How to return a variable name and assign the value of the variable returned in c

To quote Wichert, 'I suggest that you reconsider the problem you are trying to solve and check if there might not be a better method to approach it. Perhaps using an array, map or hash table might be an alternative approach that works for you.'

Using String as Variable Name (C++)

You can't use variables directly, since the name must be existent at compile time, but reading a file is runtime.

You can use std::map though. Each settings will be a key, and the value will be read in.

std::map<std::string, std::string> settings;
settings["volume"] = "76";

But, the values ("76" in this case) will be strings as well. You can not use differing types directly.
I think, its possible with type erasure, but thats really advanced (and iam not fluent with it!).

Programmatic way to get variable name in C?

You could try something like this:

#define DUMP(varname) fprintf(stderr, "%s = %x", #varname, varname);

I used to use this header I wrote, when I was new to C, it might contain some useful ideas. For example this would allow you to print a C value and provide the format specifier in one (as well as some additional information):

#define TRACE(fmt, var) \
(error_at_line(0, 0, __FILE__, __LINE__, "%s : " fmt, #var, var))

If you're using C++, you could use the type of the passed value and output it appropriately. I can provide a much more lucrative example for how to "pretty print" variable values if this is the case.

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.

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

Convert string to variable name or variable type

No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.

If you know you're going to create a variable then create it right away:

int count;

What you might not know ahead of time is the variable's value, so you can defer that for runtime:

std::cin >> count;

If you know you're going to need a collection of variables but not precisely how many of them then create a map or a vector:

std::vector<int> counts;

Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possible nor useful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.



Related Topics



Leave a reply



Submit