0

我有来自 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)

4

1 回答 1

1

正确的关键字是enum class,也在 C++11 中采用:

[FlagsAttribute]
public enum class Features: int {
    feature1 = 1,
    feature2 = 2,
    feature3 = 4,
    feature_all = feature1 | feature2 | feature3 
};

请注意,我还删除了神秘的下划线,假设您想让这个枚举类型对其他 .NET 项目可见,并希望它符合 CLS,以便它可以被不支持无符号类型的语言使用。 int是默认值,可以省略。根据需要进行调整。

于 2013-03-10T17:35:23.073 回答