Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The enum type is unscoped prefer enum class over enum?

and I'm new to C++ and I'm wondering if anyone can help me to understand why

enum difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE };
difficulty myDiffiuclty = EASY

and

enum shipCost { FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50 };
shipCost myShipCost = BOMBER_COST;

are underlined in green? it says it prefers enum class, but when I change it to enum class then

enum class difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE };
difficulty myDiffiuclty = EASY;

enum class shipCost { FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50 };
shipCost myShipCost = BOMBER_COST;

EASY becomes underlined in red and myShipCost is underlined in green, BOMBER_COST underlined in red and CRUISER_COST is underlined in red

const int ALIEN_POINTS = 150;
int aliensKilled = 10;
int score = aliensKilled * ALIEN_POINTS;
cout << "score: " << score << endl;

enum difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE };
difficulty myDifficulty = EASY;

enum shipCost { FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50 };
shipCost myShipCost = BOMBER_COST;
cout << "\nTo upgrade my ship to a cruiser will cost "
    << (CRUISER_COST - myShipCost) << " Resource Points.\n";

system("pause");
return 0;
like image 848
cmcv123 Avatar asked Oct 23 '25 14:10

cmcv123


1 Answers

enum class difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE };
difficulty myDiffiuclty = difficulty::EASY;

enum class shipCost { FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50 };
shipCost myShipCost = shipCost::BOMBER_COST;

with enum class, implicit conversion to/from integers is removed, and you have to scope the constants in it.

This is considered an improvement. Explicitly casting it back to an integer still works, but it doesn't happen by accident.

std::cout << "\nTo upgrade my ship to a cruiser will cost "
    << (static_cast<int>(shipCost::CRUISER_COST) - static_cast<int>(myShipCost)) <<
    " Resource Points.\n";

system("pause");
return 0;

once you are using enum class, you should rename them:

enum class difficulty { novice, easy, normal, hard, unbeatable };
difficulty myDiffiuclty = difficulty::easy;

enum class shipCost { fighter =25, bomber=30, cruiser = 50 };
shipCost myShipCost = shipCost::bomber;

as the ship part of the name is now in the enum, no need to repeat it in the constant. Similarly, because the names are scoped, you don't have to SHOUT them.

Note, however, if you are just using it to create constants and no intention to create a type, consider using something like:

namespace ship {
  namespace cost {
    constexpr int fighter = 25;
    constexpr int bomber = 30;
    constexpr int cruiser = 50;
  }
}

now we have ship::cost::fighter as an int that is compile-time calculated, instead of as an enum class.

like image 92
Yakk - Adam Nevraumont Avatar answered Oct 26 '25 05:10

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!