Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:58:13+00:00 2026-05-26T14:58:13+00:00

Is it a bad practice to have enums in c++ directly at namespace level?

  • 0

Is it a bad practice to have enums in c++ directly at namespace level? I mean not associated with any class? Say if I have an enum and a class that looks something like this,

enum Player { USER, COMPUTER}

class Game {
//Logic of the game.
};

So should I be declaring the Player enum as a member of game class? Should it be private?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:58:14+00:00Added an answer on May 26, 2026 at 2:58 pm

    With C++11

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

    enum class Player {
        User,
        Computer
    };
    

    The enumerated constants must be qualified with the name of the enum when referenced (e.g., Player::Computer).

    Before C++11

    No, there’s nothing inherently wrong with an enum being public. Bear in mind, however, that enumerated constants cannot be qualified with the name of the enclosing enum type. That is, you cannot write Player::USER or similar to refer to the USER constant; they appear directly in the enclosing namespace. As such, it might be a good idea to set a prefix for your constants so that no name collisions occur.

    For instance, consider the following declaration:

    enum Player {
      PL_USER,
      PL_COMPUTER
    }
    

    This is safer, because name collisions are much less likely with the “PL_” prefix. Additionally, it improves code readability by hinting at which enum a given constant belongs to.

    Languages like C# and Java have adopted a slightly different approach to enums, where one has to specify both the name of the enumeration and the name of the constant, such as Player.USER. A similar effect can be achieved in C++ by embedding the enum declaration within a namespace of its own. For example:

    namespace Player {
      enum Type {
        USER,
        COMPUTER
      }
    }
    

    This has the effect of embedding PLAYER and COMPUTER in the Player namespace instead of the global (or otherwise enclosing) namespace. Whether this is a good approach or not is, in my opinion, a matter of preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that having diamond inheritance is considered bad practice. However, I have 2
I'm wondering if it is bad practice to have a reverse proxy that selects
Would it be considered bad practice to have a viewmodel that has a property
Is it a bad practice to have a package with only one class in
Is it bad practice to have a string like name=Gina;postion= HouseMatriarch;id=1234 to hold state
Is it considered bad practice to have multiple try's in one method and structure
Is it considered bad practice to have multiple views for same URL in MVC,
I was wondering if it is bad practice to have a file_table { id,
I know they are bad practice but I have a real need on an
First off, I'm aware this is a bad practice and I have answered many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.