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 7611977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:47:55+00:00 2026-05-31T01:47:55+00:00

Should you declare enums inside or outside a class if the said enums are

  • 0

Should you declare enums inside or outside a class if the said enums are only used in the class member functions?

namespace nspace
{

// need to append OC, as this pollutes the current namespace
enum OUTSIDE_CLASS {OC_POINTS, OC_LINES, OC_LINE_LOOP, :::};
enum OTHER_ENUM {OE_POINTS};
class VertexBuffer
{
public:
    enum INSIDE_CLASS {POINTS, LINES, LINE_LOOP, :::};
    void foo(OUTSIDE_CLASS e);
    void bar(INSIDE_CLASS e);
}
};

// usage
nspace::VertexBuffer v;
v.foo(nspae::VB_POINTS);
v.bar(nspace::VertexBuffer::POINTS); // more pedantic
  • 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-31T01:47:56+00:00Added an answer on May 31, 2026 at 1:47 am

    The real goal is to avoid polluting the scope (either global or namespace) and help grouping related values together (works pretty goods with autocompletion in IDE).

    With C++11, you can declare strongly typed enums using:

    enum class MyEnum {
      Value0,
      Value1
    };
    

    which are necessarily invoked as MyEnum::Value0 (and not Value0).

    In C++03, you can more or less emulate this with:

    struct MyEnum {
      enum Type {
        Value0,
        Value1
      };
    };
    

    But then the type of the enum is MyEnum::Type which is subtly different.

    The lazy option is to just dump it in a class, but I still favor nesting a scoped enum, even within a class, just to make it clear that those values are not loose but instead are inter-related.

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

Sidebar

Related Questions

Should you declare the getters/setters of the class inside the .h file and then
I need make a class, let's say FineStack , who should declare a structure
It's known that you should declare events that take as parameters (object sender, EventArgs
How do you declare a method in C# that should be overridden (or overridable)
I guess the procedure should be something like this: declare @db varchar(100) declare @user
Should all structs and classes be declared in the header file? If I declare
Should the folders in a solution match the namespace? In one of my teams
Does Java have the possibility of anonymous enums? For example, I want my class
Should I declare an attribute in a javabean that holds a date value a
namespace myobjects { class mylifeforms { public enum Animal { dog, cat, mouse }

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.