Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

Arrays C++/CLI 在本C++中,我们可以在类定义中使用EnUm技巧: namespace EFoo { enum { a = 10; }; } class Foo { // Declare an array of 10 integers. int m_Arr[EFoo::a]; };

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

但是,使用C++/CLI中的托管枚举

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类”?然后,您可以在不使用强制转换的情况下使用它,也可以在不使用类名的情况下使用它。

从来没有想过编译器会在类定义中接受强制转换。嗯,您不是在强制转换类或类定义。您只是在强制转换枚举值。