Namespace Level Variables in C ++

Is it a bad practice to have enumerations in C ++ directly at the namespace level? I mean unrelated to any class? Say, if I have an enumeration and a class that looks something like this,

enum Player { USER, COMPUTER} class Game { //Logic of the game. }; 

So, should you declare a Player enumeration as a member of a game class? Should it be private?

+4
source share
6 answers

C ++ 11

This answer was originally written in 2011. Now that C ++ 11 support is widely available, the preferred way is to use the enum class as follows: Matthew D. Scholefield:

 enum class Player { User, Computer }; 

Enumerated constants must be specified with the name of the enumeration when referenced (for example, Player::Computer ).

Before C ++ 11

No, there is nothing wrong with the fact that the listing is public. However, keep in mind that the constants listed cannot be qualified with a type name enum enum. That is, you cannot write Player::USER or similarly to refer to the constant USER ; they appear directly in the encompassing namespace. Thus, it might be a good idea to set a prefix for your constants so that no name clashes occur.

For example, consider the following declaration:

 enum Player { PL_USER, PL_COMPUTER } 

This is safer because name collisions are much less likely with the prefix "PL_". In addition, it improves the readability of the code, hinting at which variable this constant belongs to.

Languages ​​like C # and Java have taken a slightly different approach to enumerations, where you need to specify both the enumeration name and the name of the constant, for example Player.USER . A similar effect can be achieved in C ++ by embedding an enum declaration in its own namespace. For instance:

 namespace Player { enum Type { USER, COMPUTER } } 

This results in embedding PLAYER and COMPUTER in the PLAYER namespace instead of the global (or nested) namespace. Whether this is a good approach or not is, in my opinion, a matter of preference.

+10
source

Usually use a minimal area for anything.

It makes things easier.

However, the clarity or lack of placement of this enum inside or outside the class is nothing compared to a scream, using ALL TOP TESTS . Reserve them for macros. In Java, they are used for constants because Java got its appearance from C, where it is not so unusual to define constants as macros (because early C did not have const ). Note that Java does not have a preprocessor, not macros. It is more than foolish to accept an agreement created with C for storing macros in a separate “namespace” and applying it without understanding anything else to completely cross-use the original intent.

Cheers and hth.,

+3
source

If using enum Player ...

  • only inside class Game enter private area
  • class Game and its child classes → is placed in the protected area
  • It associates with class Game and is used in various places -> put in the public domain
  • Nowhere related to Game placed in global / namespace
+2
source

It is solely a matter of preference; however, I prefer to use Java-like enums in C ++:

 class PlayerType { public: enum VALUE {USER, COMPUTER}; explicit PlayerType(VALUE val) : value_(val) {} operator VALUE() const { return value_; } bool operator==(VALUE other) const { return value_ == other; } bool operator!=(VALUE other) const { return value_ != other; } private: VALUE value_; // Copy and assign intentionally allowed. }; 

The reason I am inclined to do this is because recently it is often necessary to add additional functions (for example, converting to / from string representations), and therefore this structure simplifies the extension.

+1
source

If it is used only inside the Game class, I would put it inside it.

0
source

Better define enum in namespace

-1
source

Source: https://habr.com/ru/post/1379596/


All Articles