有没有办法通过作为用户输入的字符串来创建类实例名称?
Is there any way to create a class instance name by a string taken as user input?
在C++中,我想创建一个类实例,其名称将是一个字符串作为输入。
请考虑以下代码段。
class my_class{
int a;
};
int main()
{
string name;
getline(cin, name); //let the input "tom"
my_class name; // expecting an instance of my_class named as tom
}
应该的,我得到了错误。有什么办法可以做到吗。
"有什么办法可以做到的" - 当然:
if (name == "foo") {
/* Create a foo */
} else if (name == "bar") {
/* Create a bar */
} // Etc ....
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 从 UTF-8 字节数组创建字符串?
- 如何使用用户输入创建字符串队列?
- 通过拆分字符数组来创建字符串
- 创建字符串数组的有效方法
- 创建字符串流副本
- 有没有一种方法可以在不复制数据的情况下从string_view创建字符串流
- 如何在 cocos2d-x 3.17v 中创建字符串数组
- 如何在不迭代的情况下从string_view向量创建字符串向量?
- 用于创建字符串的可变参数模板函数
- 如何在 VBA/Excel 中创建字符串数组并将其发送到C++ DLL,以便可以在 DLL 中循环访问它
- C++宏创建字符串数组
- 创建字符串数据串起
- 从几个变量中创建字符串
- 谷歌测试:无法从字符数组创建字符串
- 是否可以创建字符串和原子<int>键值对的unordered_map?
- RegSetValueEx 不会创建字符串值
- 如何创建字符串表
- 为什么 new 能够创建字符串数组
- 创建字符串 c++ 的常量集集合