C++ 中字符串类型的枚举 class
enum class of type string in C++
- 背景资料:
在 C++11 中有一个 class,称为枚举 class,您可以在其中存储变量。但是,我只看到 class 的类型是 char:
enum class : char {
v1 = 'x', v2 = 'y'
};
- 问题:
有什么方法可以表达这个字符串类型的枚举 class?
例如,
enum class : string{
v1 = "x", v2 = "y"
};
- 我的想法:
我尝试使用它,但出现错误,我不确定我是否做对了。我之所以要使用字符串,是因为它们能够同时保存多个字符,所以这对我的代码更有用。
在 C++11 或 C++14 中无法做到这一点。但是,您应该考虑使用一些 enum class,然后编写一些显式函数或运算符以将其与 std::string
-s 相互转换。
There is a class in C++11 known as enum class which you can store variables inside.
该措辞不正确:枚举 class 不存储变量(但 enumerators)。
所以你可以编码:
enum class MyEnum : char {
v1 = 'x', v2 = 'y'
};
(这是可能的,因为,因为char
是一个整数类型;当然你不能用字符串代替)
然后定义一些 MyEnum string_to_MyEnum(const std::string&);
函数(如果参数是一个意外的字符串,它可能会抛出一些异常)和另一个 std::string MyEnum_to_string(MyEnum);
函数。你甚至可以考虑也有一些 cast operator calling them (but I don't find that readable, in your case). You could also define a class MyEnumValue
containing one single data member of MyEnum
type and have that class having cast operator,例如
class MyEnumValue {
const MyEnum en;
public:
MyEnumValue(MyEnum e) : en(e) {};
MyEnumValue(const std::string&s)
: MyEnumValue(string_to_MyEnum(s)) {};
operator std::string () const { return MyEnum_to_string(en);};
operator MyEnum () const { return en };
//// etc....
};
有了 MyEnumValue
中的更多内容(参见 rule of five),您可能几乎总是使用 MyEnumValue
而不是 MyEnum
(这甚至可能是 class MyEnumValue
)
不,这不可能。
http://en.cppreference.com/w/cpp/language/enum 状态:
The values of the constants are values of an integral type known as the underlying type of the enumeration.
关键点是 "integral type" -- 字符串不是整数类型。
编译器在内部将您的 char 转换为其等效的 int 表示形式 (ASCII)。所以不可能改用字符串。
- 背景资料:
在 C++11 中有一个 class,称为枚举 class,您可以在其中存储变量。但是,我只看到 class 的类型是 char:
enum class : char {
v1 = 'x', v2 = 'y'
};
- 问题:
有什么方法可以表达这个字符串类型的枚举 class?
例如,
enum class : string{
v1 = "x", v2 = "y"
};
- 我的想法:
我尝试使用它,但出现错误,我不确定我是否做对了。我之所以要使用字符串,是因为它们能够同时保存多个字符,所以这对我的代码更有用。
在 C++11 或 C++14 中无法做到这一点。但是,您应该考虑使用一些 enum class,然后编写一些显式函数或运算符以将其与 std::string
-s 相互转换。
There is a class in C++11 known as enum class which you can store variables inside.
该措辞不正确:枚举 class 不存储变量(但 enumerators)。
所以你可以编码:
enum class MyEnum : char {
v1 = 'x', v2 = 'y'
};
(这是可能的,因为char
是一个整数类型;当然你不能用字符串代替)
然后定义一些 MyEnum string_to_MyEnum(const std::string&);
函数(如果参数是一个意外的字符串,它可能会抛出一些异常)和另一个 std::string MyEnum_to_string(MyEnum);
函数。你甚至可以考虑也有一些 cast operator calling them (but I don't find that readable, in your case). You could also define a class MyEnumValue
containing one single data member of MyEnum
type and have that class having cast operator,例如
class MyEnumValue {
const MyEnum en;
public:
MyEnumValue(MyEnum e) : en(e) {};
MyEnumValue(const std::string&s)
: MyEnumValue(string_to_MyEnum(s)) {};
operator std::string () const { return MyEnum_to_string(en);};
operator MyEnum () const { return en };
//// etc....
};
有了 MyEnumValue
中的更多内容(参见 rule of five),您可能几乎总是使用 MyEnumValue
而不是 MyEnum
(这甚至可能是 class MyEnumValue
)
不,这不可能。
http://en.cppreference.com/w/cpp/language/enum 状态:
The values of the constants are values of an integral type known as the underlying type of the enumeration.
关键点是 "integral type" -- 字符串不是整数类型。
编译器在内部将您的 char 转换为其等效的 int 表示形式 (ASCII)。所以不可能改用字符串。