enum class as array index
Scoped enum
s (enum class
) are not implicitly convertible to integers. You need to use a static_cast
:
SDL_Surface*KEY_PRESS_SURFACES[static_cast<int>(KeyPressSurfaces::KEY_PRESS_SURFACE_TOTAL)];
Remove the class
keyword or cast explicitly to an integral type.
You can convert your enum
to int
using template function and you will get more readable code:
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
enum class KeyPressSurfaces: int {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
template <typename E>
constexpr typename std::underlying_type<E>::type to_underlying(E e) {
return static_cast<typename std::underlying_type<E>::type>(e);
}
int main() {
KeyPressSurfaces val = KeyPressSurfaces::KEY_PRESS_SURFACE_UP;
int valInt = to_underlying(val);
std::cout << valInt << std::endl;
return 0;
}
I fount to_underlying
function here