Search code examples
c++introspection

Is it possible in C++ to access a variable based on its name (introspection)


I was wondering if this is possible to do in C++. I have a function that takes user input for x and y bounds and I need to verify it, and it would be easier to do with one function. Is this possible in C++? Here's some pseudocode.

void bounds(char i){
  // if i is 'x' 
  std::cin >> [i]Lower    // store to xLower
  // verify
}
// then do
bounds('x');
bounds('y');

Solution

  • No, C++ code cannot do anything which depends on the variables name. e.g. If your variable is named x or y, it won't affect the code's behaviour.