0
int a = 1;
int b = 2;
int c = 3;
std::string TakeFrom = "a";

How can I get the value from TakeFrom, in this case, a, and read the integer named like that? For example, in the code above I would have to read a's value because 'a' is what TakeFrom contains, so I'd get "1", but if TakeFrom contained "b", I'd get b's value (2).

1
  • You can't exactly, but you could use a std::map to map strings (variable names) to ints. Commented Nov 1, 2013 at 20:42

3 Answers 3

1

In normal C/C++ conditions, variable names only exist for their given scope during compilation, they are essentially human-readable mnemonics for referencing specific memory locations/registers.

C/C++ has no-to-minimal reflection capabilities, so there is no built-in way, at runtime, to reference a variable by name or look up the name of a variable by it's location/value.

To solve the problem you are describing, there are several simple options:

int value;
if (TakeFrom == "a") {
     value = a;
} else if (TakeFrom == "b") {
     value = b;
} ...

or

switch (TakeFrom[0]) {
    case 'a':
        value = a;
    break;
    case 'b':
        value = b;
    break;
}

or you could use a lookup table

int a = 1, b = 2, c = 3;
struct Lookup {
    int&         m_variable;
    const char*  m_name;
};
static const size_t NumLookups = 3;
Lookup lookups[NumLookups] = { { a, "a" }, { b, "b" }, { c, "c" } };
for (size_t i = 0; i < NumLookups; ++i) {
    if (TakeFrom == lookups[i].m_name)
        value = lookups[i].m_variable;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If a, b, and c must stay the local ints, the only way you do it is the boring one, i.e. like this:

int val;
if (TakeFrom == "a") {
    val = a;
} else if (TakeFrom == "b") {
    val = b;
} else if (TakeFrom == "c") {
    val = c;
} else {
    // It's an error
}

If you are open to some changes, there are several things that you could do:

  • If you are willing to "encode" the TakeFrom, making it an integer instead of a string (say, 0 means a, 1 means b, and 2 means c), you could place a, b, and c into an array, and use the integer TakeFrom as an index.
  • If you are willing to allow only single-character variables, you could replace the sequence of conditionals with a single switch.
  • If you are open to using standard collections, replace the individual variables with a map of string to integer.

1 Comment

Hello, sorry for late replying. Thanks for the answer. It's the most complete, and it's what I had to do.
0

With there separate variables, there's no easy way (other than a series of if statements or equivalent).

If you turned the three variables into a single std::map<std::string,int>, things would be a lot easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.