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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:20:46+00:00 2026-05-31T16:20:46+00:00

Can someone shed some light on the semantics for accessing an enum defined in

  • 0

Can someone shed some light on the semantics for accessing an enum defined in a class in C++?

In particular, why are enum members accessed by the name of the class rather than the enum itself? Given that the enum is the container/scope, just as namespace and class are, why is accessing an element of the container treated differently when it’s an enum than when it’s a class?

Given

namespace mynamespace
{
    class myclass
    {
    public:
        enum myenum
        {
            enum1,
            enum2
        };

        int myint;
    };
}

Why is the fully-qualified name for enum1 mynamespace::myclass::enum1 and not mynamespace::myclass::myenum::enum1?

While the latter “works,” it’s not the “recommended” way of calling it and some compilers will throw a warning there. IMHO, it should not only be right, but it should also be the only way of accessing it.

It makes for really odd access rules, and makes things very weird when you add a new enum1 in a different enum (at which point you must add the qualifier).

Really, it defeats the purpose of an enum. The members of the enum are really more members of the class than they are of the enum, and I must say I find the behavior in other languages (e.g. C#) to be much more preferable.

I imagine this is to keep compatibility with C, but I don’t see why requiring the enum name in the access semantic would be the better option… I’d imagine making the class name optional would be the option that preserves C compatibility.

  • 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-31T16:20:47+00:00Added an answer on May 31, 2026 at 4:20 pm

    In C++03, as in C, an enum does not introduce a new scope. The names defined go into the surrounding scope. Which might be a namespace, or a class.

    C++11 added scoped enums and based enums.

    Ordinary C++03 enum:

    enum Cpp03 { a, b, c };
    

    C++11 based enum:

    enum Cpp11Based: long { a, b, c };
    

    C++11 scoped enum (which acts as a scope for the names, like in your example):

    enum class Cpp11Scoped1 { a, b, c };
    enum struct Cpp11Scoped2 { a, b, c };
    

    The last two forms are equivalent, and allow writing e.g. Cpp11Scoped1::a.

    Finally, a scoped enum can be based (specifying the underlying type for the names, i.e. size and signedness):

    enum class Cpp11ScopedAndBased: long { a, b, c };
    

    Some C++03 compilers, including Visual C++, provided the scope functionality also for the ordinary C++03 enum.

    My guess is that what you have run into, a language extension.

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

Sidebar

Related Questions

Can someone please shed some light on how I would get this to work:
Hoping someone can shed some light on this: Do lookup tables need their own
I'm hoping someone can shed some light on this for me: What are the
I am hoping someone can shed some light on this issue for me... I
I wonder if someone can help shed some light on this: I drop a
I hope someone out there can shed some light on the topic of creating
Can someone shed some light on how Unity automatically does dependency graph resolution? I
Can someone shed some light on how searching is done on web-sites like del.icio.us?
Can someone shed some light on this issue? The expected result does NOT appear
This question is really just out of curiosity. Can someone shed some light onto

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.