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