enum subsets

Nov 15, 2008 at 2:16pm
hi,
I was wondering if it was possible to use multiple enums that are subsets of eachother. This next example is syntactically incorrect, but it's what I do want to acheave:

typedef enum one
{
A,
B,
C,
D
}

typedef enum two
{
C,
D,
E
}

and also,
is it possible to re-use any of the enum elements as variables, but still retaining its meaning, being part of an enum.
like:

void somthing(one element1,two element2)
{

}

int main()
{
int A = 1;

something(A,E);
}

thanks!
Nov 15, 2008 at 9:54pm
No. Enums are not "full" types, which is annoying, which means that

1
2
3
4
5
6
7
8
9
enum RedThings {
   Apple,
   Tomato
};

enum Fruit {
   Orange,
   Apple
};


declares the symbol "Apple" twice, the first time with value 0 and the second with value 1. Symbols declared "inside" enums are not scoped within the enum. That is, you don't refer to Fruit::Apple, but rather just Apple.

Nov 16, 2008 at 7:35pm
@jsmith: Yes, it's one of the pitfalls in C++ really isn't it.

You can always do.

1
2
3
4
5
6
namespace Fruit {
 enum Fruit {
  Apple,
  Orange
 };
}


Perhaps even having a macro similar to how boost have defined their BOOST_FOREACH one would be handy :)
Nov 16, 2008 at 8:58pm
Yup, sometimes I wish I could inherit enums :)

1
2
3
4
5
6
7
8
9
enum ErrorCode {
    Success,
    UnspecifiedFailure
};

enum FileError : public ErrorCode {
    FileNotFound,
    DiskFull
};


etc...

EDIT: Oh, and use them for bit masking too :)

1
2
3
4
5
6
7
enum BitFlags {
    Flag1 = 0x01,
    Flag2 = 0x02,
    Flag3 = 0x04
};

BitFlags bf = Flag1 | Flag2;


Last edited on Nov 16, 2008 at 9:00pm
Topic archived. No new replies allowed.