Avatar ·

Why is enum class considered safer than regular enum in C++?

Hello everyone! I recently started learning C++ and came across enumerations. I know there are two ways to declare them: the good old `enum` and the new `enum class` (since C++11). I've read that `enum class` is "safer", but I don't fully understand the practical difference. Here's an example I wrote: ```cpp enum Color { RED, GREEN, BLUE }; enum TrafficLight { RED, GREEN, YELLOW }; // Error: redeclaration of RED and GREEN int main() { Color c = RED; TrafficLight t = RED; // I expected this to work, but... return 0; } ``` I immediately ran into problems: I can't declare two enumerations with the same element names, and the compiler complains. Additionally, I noticed that I can write something like: ```cpp int x = RED; // This works, but is this normal?