简体   繁体   English

将枚举数组转换为int指针

[英]cast an array of enum to pointer of int

I'm getting the complaint from complier when trying to convert a array of enum to an pointer of int. 尝试将枚举数组转换为int指针时,我收到编译器的抱怨。

void format(const int *values);
// convert and call format
format(static_cast<const int*>(EnumArray));

// error from compiler
error: invalid static_cast from type 'const EnumArray[15]' to type 'const int*'

Any way to get around it? 有办法解决吗? Thanks! 谢谢!

Seems I can solve it with template. 似乎我可以用模板解决它。 It compiles and runs. 它编译并运行。

    template<typename T>
    String8  format(const T *values)
    {
          //use values as array of int
          int v = values[i];
    }

    //call it
    format(EnumArray); // no type needed since it can be deduced

如果您完全知道自己在做什么,则可以使用reinterpret_cast

format(reinterpret_cast<const int*>(EnumArray));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM