2010-12-14 66 views
0

在本地C++中,我们可以在类定义中使用枚举招:枚举招用C++/CLI

namespace EFoo 
{ 
    enum { a = 10; }; 
} 

class Foo 
{ 
    // Declare an array of 10 integers. 
    int m_Arr[EFoo::a]; 
}; 

然而,在C++/CLI管理枚举,

public enum class EFoo 
{ 
    a = 10, 
}; 

EFoo ::一个couldn不会被隐式转换为int,因此枚举技巧将不被允许。

有什么解决方法吗?

谢谢。

回答

2

尝试:

arr[(int)EFoo.a]; 
+0

永远不要认为编译器接受类定义中的转换。 – Wilson 2010-12-14 23:48:56

+0

那么,你不是在投射类或类的定义。你只是施放枚举值。 – 2010-12-14 23:52:43

4

如果你只是想实现“enum黑客”,你不应该这样做,在任何近期的编译器,因为它们将支持static const成员声明。

class Foo 
{ 
private: 
    static const int ARRAY_SIZE = 10; 
    int m_arr[ARRAY_SIZE]; 
}; 

否则,做一个int铸像乔纳森·伍德回答将努力从管理enum更改为int

1

如果你不需要enacpsulation,为什么不把它声明为“enum”而不是“enum class”?然后,您可以在没有演员的情况下使用它,而且也可以不使用类名。