Sign in to follow this  
Master Jake

Enumerations Inside Classes

Recommended Posts

Master Jake    100
I was thinking about embedding an enumeration inside one of my classes. I don't have a problem per se, just a question regarding "proper" use here. This is the method I've mainly been using:

[code]
class A
{
public:
static const enum
{
FIRST_VALUE = 0,
SECOND_VALUE = 1
};
};

// use
A::FIRST_VALUE
A::SECOND_VALUE
[/code]

My questions are:
1. Is it okay to declare anonymous structures in classes, or should it have a name (even though I'll never use that name)?
2. Should I make the enum const? I've no difference between using it or not, but it makes sense for me to use it since the enumeration values are never changing.

Share this post


Link to post
Share on other sites
Wooh    1088
1. I don't think anonymous structures are allowed in C++
2. It is not valid code to write 'const enum'. You can never change the enum values anyway. They are compile time constants.

Share this post


Link to post
Share on other sites
Master Jake    100
[quote name='Wooh' timestamp='1306865898' post='4817963']
1. I don't think anonymous structures are allowed in C++
2. It is not valid code to write 'const enum'. You can never change the enum values. They are compile tile constants.
[/quote]

I have gotten code using anonymous enumerations (inside a class at least) to compile fine in Visual Studio. Your point about const does make sense though.

[b]Edit:[/b] I've also gotten away with not even using the static keyword since I'm not declaring an actual instance but a full-blown type.

Share this post


Link to post
Share on other sites
Texus    248
I do agree with your second point that you can never change enum values, but it is possible to work with anonymous structures.

EDIT: it seems that Master Jake answered at the same time than me.

Share this post


Link to post
Share on other sites
Wooh    1088
Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.

Share this post


Link to post
Share on other sites
Texus    248
[quote name='Wooh' timestamp='1306866556' post='4817969']
Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.
[/quote]
I just tested it and even anonymous structures are valid (inside classes, not outside).

Share this post


Link to post
Share on other sites
Master Jake    100
[quote name='Wooh' timestamp='1306866556' post='4817969']
Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.
[/quote]

Sorry about that, I think I confused myself when typing it. Thanks for the help though :)

Share this post


Link to post
Share on other sites
Wooh    1088
[quote name='Texus' timestamp='1306866745' post='4817972']
I just tested it and even anonymous structures are valid (inside classes, not outside).
[/quote]
It is probably a compiler extension. In gcc I get [i]error: ISO C++ prohibits anonymous structs[/i]
EDIT: I found something called unnamed structs that is allowed. Now I'm confused what the difference is.

Share this post


Link to post
Share on other sites
iMalc    2466
To declare an enum then you should use neither static, nor const. You're not actually creating an instance of anything here.

When declaring it inside a class, to me the reason for doing so would be that it is not used outside of that class, so you would not be referring to them as AA[color="#666600"]::[/color]FIRST_VALUE etc since they would only be used internally. If you do use them outside the class, then I would either declare them outside of the class, or perhaps make it a namespace instead of a class.

Share this post


Link to post
Share on other sites
Master Jake    100
[quote name='iMalc' timestamp='1306869630' post='4817986']
To declare an enum then you should use neither static, nor const. You're not actually creating an instance of anything here.

When declaring it inside a class, to me the reason for doing so would be that it is not used outside of that class, so you would not be referring to them as AA[color="#666600"]::[/color]FIRST_VALUE etc since they would only be used internally. If you do use them outside the class, then I would either declare them outside of the class, or perhaps make it a namespace instead of a class.
[/quote]

I actually did use the namespace method. I was originally declaring it inside the class because it was a Keyboard class and the enumeration was for key states, and I just wanted to compile them together because it made sense to me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this