What is the purpose of anonymous enum declarations such as:
enum { color = 1 };
Why not just declare int color = 1?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Enums don’t take up any space and are immutable.
If you used
const int color = 1;then you would solve the mutability issue but if someone took the address ofcolor(const int* p = &color;) then space for it would have to be allocated. This may not be a big deal but unless you explicitly want people to be able to take the address ofcoloryou might as well prevent it.Also when declaring a constant field in a class then it will have to be
static const(not true for modern C++) and not all compilers support inline initialization of static const members.Disclaimer: This answer should not be taken as advice to use
enumfor all numeric constants. You should do what you (or your co-workers) think is more readable. The answer just lists some reasons one might prefer to use anenum.