Arrays C++/CLI 在本C++中,我们可以在类定义中使用EnUm技巧: namespace EFoo { enum { a = 10; }; } class Foo { // Declare an array of 10 integers. int m_Arr[EFoo::a]; };
但是,使用C++/CLI中的托管枚举Arrays C++/CLI 在本C++中,我们可以在类定义中使用EnUm技巧: namespace EFoo { enum { a = 10; }; } class Foo { // Declare an array of 10 integers. int m_Arr[EFoo::a]; };,arrays,enums,c++-cli,Arrays,Enums,C++ Cli,但是,使用C++/CLI中的托管枚举 public enum class EFoo { a = 10, }; EFoo::a无法隐式转换为int, 所以枚举技巧是不允许的 有什么解决办法吗 谢谢。试试: arr[(int)EFoo.a]; 如果您只是想实现“enumhack”,那么您不必在最近的任何编译器中这样做,因为它们将支持static const成员声明 class Foo { private: static const int ARRAY_SIZE = 10; i
public enum class EFoo
{
a = 10,
};
EFoo::a无法隐式转换为int,
所以枚举技巧是不允许的
有什么解决办法吗
谢谢。试试:
arr[(int)EFoo.a];
如果您只是想实现“
enum
hack”,那么您不必在最近的任何编译器中这样做,因为它们将支持static const
成员声明
class Foo
{
private:
static const int ARRAY_SIZE = 10;
int m_arr[ARRAY_SIZE];
};
否则,像Jonathan Wood回答的那样执行
int
强制转换可以将托管enum
更改为int
如果不需要enacpsulation,为什么不将其声明为“enum”而不是“enum类”?然后,您可以在不使用强制转换的情况下使用它,也可以在不使用类名的情况下使用它。从来没有想过编译器会在类定义中接受强制转换。嗯,您不是在强制转换类或类定义。您只是在强制转换枚举值。