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

  • Home
  • SEARCH
  • 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 115307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:59:54+00:00 2026-05-11T02:59:54+00:00

Often, one needs several enumerated types together. Sometimes, one has a name clash. Two

  • 0

Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use ‘larger’ enum element names. Still, the namespace solution has two possible implementations: a dummy class with nested enum, or a full blown namespace.

I’m looking for pros and cons of all three approaches.

Example:

// oft seen hand-crafted name clash solution enum eColors { cRed, cColorBlue, cGreen, cYellow, cColorsEnd }; enum eFeelings { cAngry, cFeelingBlue, cHappy, cFeelingsEnd }; void setPenColor( const eColors c ) {     switch (c) {         default: assert(false);         break; case cRed: //...         break; case cColorBlue: //...         //...     }  }   // (ab)using a class as a namespace class Colors { enum e { cRed, cBlue, cGreen, cYellow, cEnd }; }; class Feelings { enum e { cAngry, cBlue, cHappy, cEnd }; }; void setPenColor( const Colors::e c ) {     switch (c) {         default: assert(false);         break; case Colors::cRed: //...         break; case Colors::cBlue: //...         //...     }  }    // a real namespace?  namespace Colors { enum e { cRed, cBlue, cGreen, cYellow, cEnd }; };  namespace Feelings { enum e { cAngry, cBlue, cHappy, cEnd }; };  void setPenColor( const Colors::e c ) {     switch (c) {         default: assert(false);         break; case Colors::cRed: //...         break; case Colors::cBlue: //...         //...     }   } 
  • 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. 2026-05-11T02:59:55+00:00Added an answer on May 11, 2026 at 2:59 am

    Original C++03 answer:

    The benefit from a namespace (over a class) is that you can use using declarations when you want.

    The problem with using a namespace is that namespaces can be expanded elsewhere in the code. In a large project, you would not be guaranteed that two distinct enums don’t both think they are called eFeelings

    For simpler-looking code, I use a struct, as you presumably want the contents to be public.

    If you’re doing any of these practices, you are ahead of the curve and probably don’t need to scrutinize this further.

    Newer, C++11 advice:

    If you are using C++11 or later, enum class will implicitly scope the enum values within the enum’s name.

    With enum class you will lose implicit conversions and comparisons to integer types, but in practice that may help you discover ambiguous or buggy code.

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

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I don't usually use 1 to 1 relations unless there… May 11, 2026 at 7:25 am
  • added an answer To define a generic class that handles any child of… May 11, 2026 at 7:25 am
  • added an answer I'm assuming you're developing on the server-side, some kind of… May 11, 2026 at 7:25 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.