1

I have an enum but I intend to have the enum itself as an array that I can walk through as a list:

#include <iostream>
using namespace std;
typedef enum p_states {
    ALL,
    SEMI,
    COMBO1,
}states;
states s;
int main()
{
    s = ALL;
    cout<<s[0]<<endl;
    cout<<s[1]<<endl;
    cout<<s[2]<<endl;

}

Which results in the errors:

enum_states.cpp: In function ‘int main()’:
enum_states.cpp:12:11: error: invalid types ‘states {aka p_states}[int]’ for array subscript
  cout<<s[0]<<endl;
           ^
enum_states.cpp:13:11: error: invalid types ‘states {aka p_states}[int]’ for array subscript
  cout<<s[1]<<endl;
           ^
enum_states.cpp:14:11: error: invalid types ‘states {aka p_states}[int]’ for array subscript
  cout<<s[2]<<endl;
           ^

I can only think of having an array states.

s[3] = {ALL,SEMI,COMBO1};

Is there a better way of defining this only once and have it as an enum?

Kninnug
  • 7,992
  • 1
  • 30
  • 42
preetam
  • 1,451
  • 1
  • 17
  • 43
  • If you haven't assigned explicit values to the enum, you could fill an array from the first enum value to the last enum value, increasing by one each time. Otherwise, you could consider writing an enumeration class. – David Zech Aug 14 '15 at 23:56
  • 2
    Why do you need to do this? What are you trying to accomplish? – Emil Laine Aug 15 '15 at 00:01
  • @zenith To get a list of all the enums? Like Java's .getAll() or C#'s Enum.GetValues(). I'd guess an use case is an enumeration of permissions, and you'd want an easy way to assign universal access. – David Zech Aug 15 '15 at 00:05
  • That's not possible with C++ enums. You have to do it manually. C++ enums are fundamentally different from Java's and C#'s enums. – Emil Laine Aug 15 '15 at 00:06
  • 1
    To me this sounds like an XY problem. What are you _really_ trying to do in your code? Are you trying to use enums as _flags_? – Emil Laine Aug 15 '15 at 00:11
  • I need to implement power states and have them addressed as enums. So basically I have x states which are implemented in a state machine. The states are indicated by the enum. However, I need to walk through the states to identify the current power mode. I just want a clean way of doing it. But I the enums are basically predefined value sets. So they are not really an array of values. This makes it difficult to walk through them. But I guess I can use a comparison operator as long as the values are sequential. – preetam Aug 17 '15 at 17:17

2 Answers2

1

If you're trying to use enums as flags, you can specify the underlying values of each enumerator to be able to use them as a flag set with the help of bitwise operators:

enum permission
{
    ReadPermission = 1,
    WritePermission = 2,
    ExecutePermission = 4,
    // etc.
    FullPermissions = ReadPermissions | WritePermissions | ExecutePermission
};

bool hasReadPermission(permission P)
{
    return P & ReadPermission;
}

bool hasWritePermission(permission P)
{
    return P & WritePermission;
}

void addReadPermission(permission& P)
{
    P |= ReadPermission;
}

void removeReadPermission(permission& P)
{
    P &= ~ReadPermission;
}

…

int MyPermissions = FullPermissions;

assert(hasReadPermission(MyPermissions));
assert(hasWritePermission(MyPermissions));

removeReadPermission(MyPermissions);

assert(!hasReadPermission(MyPermissions));
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

You can't treat an enum as an array. In your case, there is no need. Your enumerations are sequential from 0. So long as you know your integer value is in bounds, you can simply assign the index value to a variable of your enumerated type. However, the output would just reflect the integer itself, not the symbolic symbol.

If you are willing to use X macros, you can easily create parallel data structures from a single list of states. One of the data structures could be an array.

#define P_STATES_X \
        X(ALL)     \
        X(SEMI)    \
        X(COMBO1)

enum p_states {
    #define X(E) E,
    P_STATES_X
    #undef X
    P_STATES_MAX
};

std::string p_states_strings[] = {
    #define X(E) #E,
    P_STATES_X
    #undef X
};

//...
    for (int i = 0; i < P_STATES_MAX; ++i)
        std::cout << p_states_strings[i] << '\n';
Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193
  • I like the idea of X-macros but I am not certain that the qnx compiler allows for it. Let me try it though. – preetam Aug 17 '15 at 17:21