Why declaration of two enums type with same values in same block is not allowed in C++?
enum math_students {A,B,C};
enum comp_students {D,E,A}; // illegal
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.
C++03
enumdoesn’t have tighter type checking under their scopes. So bothmath_students::Aandcomp_students::Acan be simply referred asA. That’s why they are not allowed in the same scope.To overcome that you can enclose them in
namespaceorclass.In C++11 you can use
enum class(it has tighter type checking; and they don’t get converted tointimplicitly).