简体   繁体   English

c ++中的类型字符串的枚举类

[英]enum class of type string in C++

- Background Information: - 背景资料:

There is a class in C++11 known as enum class which you can store variables inside. C ++ 11中有一个类称为枚举类,您可以在其中存储变量。 However, I have only seen the type of the class to be char: 但是,我只看到类的类型为char:

enum class : char {
   v1 = 'x', v2 = 'y'
};

- Question: - 题:

Is there any way I can express this enum class of type string? 有什么方法可以表达这个类型字符串的枚举类吗?

For example, 例如,

enum class : string{
  v1 = "x", v2 = "y"
};

- What I think: - 我的想法:

I tried using it, but I got errors, I am not sure if I am doing it right or not. 我尝试使用它,但是我遇到了错误,我不确定我是否做得对。 The reason why I want to use strings is that they are capable of holding multiple characters at the same time, so it makes them more useful for my code. 我想使用字符串的原因是它们能够同时保存多个字符,因此它对我的代码更有用。

There is no way to do that in C++11 or C++14. 在C ++ 11或C ++ 14中无法做到这一点。 However, you should consider having some enum class , then code some explicit functions or operators to convert it from and to std::string -s. 但是,你应该考虑使用一些枚举类 ,然后编写一些显式函数或运算符来将它转换为std::string -s。

There is a class in C++11 known as enum class which you can store variables inside. C ++ 11中有一个类称为枚举类,您可以在其中存储变量。

That phrasing is not correct: an enum class does not store variables (but enumerators ). 这句话不正确:枚举类不存储变量(而是枚举器 )。

So you might code: 所以你可能编码:

enum class MyEnum : char {
   v1 = 'x', v2 = 'y'
};

(this is possible, as answered by druckermanly , because char is an integral type ; of course you cannot use strings instead) (这是可能的,正如druckermanly回答的那样 ,因为char是一个完整的类型 ;当然你不能使用字符串代替)

then define some MyEnum string_to_MyEnum(const std::string&); 然后定义一些MyEnum string_to_MyEnum(const std::string&); function (it probably would throw some exception if the argument is an unexpected string) and another std::string MyEnum_to_string(MyEnum); function(如果参数是一个意外的字符串,它可能会抛出一些异常)和另一个std::string MyEnum_to_string(MyEnum); one. 一。 You might even consider also having some 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 , eg 您还可以定义一个class MyEnumValue包含一个MyEnum类型的单个数据成员,并且该类具有MyEnum 操作符 ,例如

 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....
 };

With appropriate more things in MyEnumValue (see the rule of five ) you might almost always use MyEnumValue instead of MyEnum (which perhaps might even be internal to class MyEnumValue ) MyEnumValue有更多的东西(参见五条规则 ),你几乎总是可以使用MyEnumValue而不是MyEnum (它可能甚至可能是class MyEnumValue

No, this is not possible. 不,这是不可能的。

http://en.cppreference.com/w/cpp/language/enum states: 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. 常量的值是整数类型的值,称为枚举的基础类型。

The key point being "integral type" -- a string is not an integral type. 关键点是“整数类型” - 字符串不是整数类型。

Compiler internally converts your char to its equivalent int representation (ASCII). 编译器在内部将您的char转换为其等效的int表示(ASCII)。 So it would not be possible to use string instead. 所以不可能使用字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM