Enumerations Inside Classes

Started by
8 comments, last by Master Jake 12 years, 10 months ago
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:


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

// use
A::FIRST_VALUE
A::SECOND_VALUE


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.
Advertisement
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.

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.


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.

Edit: 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.
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.
TGUI, a C++ GUI for SFML
texus.me
Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.

Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.

I just tested it and even anonymous structures are valid (inside classes, not outside).
TGUI, a C++ GUI for SFML
texus.me

Yes, anonymous enumerations are valid. You said 'anonymous structures' so I thought you meant a struct without a name.


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

I just tested it and even anonymous structures are valid (inside classes, not outside).

It is probably a compiler extension. In gcc I get error: ISO C++ prohibits anonymous structs
EDIT: I found something called unnamed structs that is allowed. Now I'm confused what the difference is.
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"]::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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

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"]::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.


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.

This topic is closed to new replies.

Advertisement