开发者

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

开发者 https://www.devze.com 2023-01-01 06:17 出处:网络
This question already has answers here: Convert string to variable name or variable type (7 answers) Closed 6 years ago.
This question already has answers here: Convert string to variable name or variable type (7 answers) Closed 6 years ago.

If the title was not clear, I will try to clarify what I am asking:

Imagine I have a variable called counter, I know I can see its current value by doing something like:

std::cout << counter << std::endl;

However, assume I have lots of variables and I don't know which I'm going to want to look at until runtime.

Does anyone know a way I can fetch the value of a variable by using its name, for example:

std::cout << valueOf("counter") << std::endl;

I feel being able to do thi开发者_Go百科s might make debugging large complex projects easier.

Thanks in advance for your time.


Update: All the answers provided are valid and useful, however the main point is that reflection does not exist in C++ (and after reading the link recommended it is clear why).


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


You can use an associative container as a std::map< std::string, your_variable_type > to link a string to a variable, assuming they are all of the same type.

If you have variable of different types, solution exists, as in boost::variant


No, not with C++ or its standard library. Of course, you can hack something up to emulate this behaviour. C++ allows you to choose methods at runtime, using polymorphism, so you can take advantage of that. In essence, you'll get the method to invoke at runtime, rather than the variable, and the method will return the vlaue:

struct Value {
    virtual ~Value();
    virtual std::string value() const = 0;
};

struct Counter : public Value {
    int v;
    std::string value() const {
        istringstream ss(v);
        return ss.str();
    }
};

struct Mounter : public Value {
    double v;
    std::string value() const {
        istringstream ss(v);
        return ss.str();
    }
};

Value* get_value();

// ...
cout << get_value()->value() << endl;

Alternatively, you can maintain a map keyed on strings, the names of the values, and then look up the values with their names.


C++ addresses all variables via address so there is no way of just saying valueOf a variable (Languages which allow this e.g. python, perl have a runtime keeping this information)

You can implement something allowing use of a name to find a value by storing the values and their names in a std::map.


Well if you really want to do something like this, you might try using std::map (map< string, int* > ). Hovewer this would restrict you to one variable type, unless you delve into some ugly pointer magic. Either way it will be ugly and trust me you really don't want to go down this path. Why do you even need such feature? If for debugging purposes, use a debugger.


As said already, C++ provides no reflection. But you can use some key/value mapping on your own. When only one value type (e.g. int) is required, you can use an STL associative container (map) out of the box. If you need to support multiple value types, i recommend a look into boosts Variant library.


This would need something akin to reflection or eval which C++ doesn't have.


No, C++ does not provide that facility. You may be able to hack something together with macros (or other trickery) but it's likely to be fairly ugly.

Since I can think of no good reason of the top of my head why this would be useful, perhaps you could enlighten us. When something cannot be done in a certain way, it's often good to step back to the base requirements and see if there's another way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号