簡體   English   中英

將枚舉數組轉換為int指針

[英]cast an array of enum to 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*'

有辦法解決嗎? 謝謝!

似乎我可以用模板解決它。 它編譯並運行。

    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