开发者

Enum trick in C++/CLI

In native C++ we could use enum trick in class definition:

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

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

However, with managed enum in C++/CLI,

开发者_如何转开发
public enum class EFoo
{
  a = 10,
};

EFoo::a couldn't be converted implicitly to int, so the enum trick wouldn't be allowed.

Is there any workaround?

Thanks.


If you are just trying to achieve the 'enum hack', you should not have to do that in any recent compiler, as they will support static const member declarations.

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

Otherwise, doing an int cast like Jonathan Wood answered would work to change from a managed enum to an int.


Try:

arr[(int)EFoo.a];


If you dont need the enacpsulation, why not declare it as an "enum" instead of the "enum class"? You can then use it without the cast, and also, without the classname .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜