我有VS2003,托管C ++的代码片段,我想用2010 C ++ / CLI方式重写它,但是新的编译器不接受'feature_all'枚举..有人能告诉我如何正确转换它?
这是VS 2003中的代码段:
[Flags]
__value enum Features: unsigned int
{
feature_1 = 1,
feature_2 = 2,
feature_3 = 4,
feature_all = feature_1 | feature_2 | feature_3 // accepted by compiler
};
我试着在VS 2010中这样写:
[FlagsAttribute]
value class enum Features: unsigned int {
feature1 = 1,
feature2 = 2,
feature3 = 4,
feature_all = feature_1 | feature_2 | feature_3 // not accepted by compiler
};
但第二个肯定不起作用......
编译器返回7个错误:(C2332,C2236,3x C2065,C2056和C3115)
答案 0 :(得分:1)
正确的关键字是枚举类,也在C ++ 11中采用:
[FlagsAttribute]
public enum class Features: int {
feature1 = 1,
feature2 = 2,
feature3 = 4,
feature_all = feature1 | feature2 | feature3
};
请注意我是如何删除神秘的下划线,假设您希望将此枚举类型设置为对其他.NET项目可见,并希望它与CLS兼容,因此它可以被不支持无符号类型的语言使用。 int 是默认值,可以省略。根据需要调整。