C++:变量中的值作为变量
C++ : value from variable as variable
如何在 C++ 中从其他变量的值打印变量我只是 C++ 的新手。
在PHP中,我们可以通过其他变量的值来制作/打印变量。喜欢这个。
$example = 'foo';
$foo = 'abc';
echo ${$example}; // the output will 'abc'
如何在 C++ 中解决此问题?
你不能
。
模拟这一点(嗯)的唯一方法是使用地图
按名称获取变量/成员称为反射/内省。
C++中没有反射机制,基本上你不能这样做。
从另一个角度来看,它只是间接的,C++广泛使用。 C++中类似的例子可能是...
using namespace std;
string foo = "abc";
string* example = &foo;
cout << *example << endl; // The output will 'abc'
。或使用引用而不是指针...
using namespace std;
string foo = "abc";
string& example = foo;
cout << example << endl; // The output will 'abc'
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 变量没有改变?通过向量的函数调用