Hayden Piper Hayden Piper - 4 years ago 321
C++ Question

enum class as array index

I made an enum as:

enum class KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};


and later on I attempt to define an array as I typed below, but I received the error,
size of array 'KEY_PRESS_SURFACES' has non-integral type 'KeyPressSurfaces'


SDL_Surface*KEY_PRESS_SURFACES[KeyPressSurfaces::KEY_PRESS_SURFACE_TOTAL];


I understand the error fine, but I don't know where to move the
KeyPressSurfaces
to qualify the constant in the enum.

I also realize I could just use an
enum
and not an
enum class
, but I feel like this should work, and I want to learn how to do this.

Any response/advice is appreciated! Thanks!

Answer Source

Scoped enums (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)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download