在 C++ 中是否可以根据名称访问变量(内省)
Is it possible in C++ to access a variable based on its name (introspection)
我想知道这是否可以在 C++ 中完成。我有一个函数接受用户输入的 x 和 y 边界,我需要验证它,使用一个函数会更容易。这在 C++ 中可能吗?这是一些伪代码。
void bounds(char i){
// if i is 'x'
std::cin >> [i]Lower // store to xLower
// verify
}
// then do
bounds('x');
bounds('y');
不,C++ 代码不能做任何依赖于变量名的事情。例如如果您的变量名为 x 或 y,它不会影响代码的行为。
我想知道这是否可以在 C++ 中完成。我有一个函数接受用户输入的 x 和 y 边界,我需要验证它,使用一个函数会更容易。这在 C++ 中可能吗?这是一些伪代码。
void bounds(char i){
// if i is 'x'
std::cin >> [i]Lower // store to xLower
// verify
}
// then do
bounds('x');
bounds('y');
不,C++ 代码不能做任何依赖于变量名的事情。例如如果您的变量名为 x 或 y,它不会影响代码的行为。