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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:21:13+00:00 2026-05-15T16:21:13+00:00

Sorry for the not very descriptive question title. I’m not very sure how to

  • 0

Sorry for the not very descriptive question title. I’m not very sure how to describe this, hopefully I can make it better later if someone can explain this to me.

I was about to come on here and ask why the following example was working. It was exhibiting the behaviour I was hoping for but I wasn’t sure why or whether it was standard or whether I just got lucky with the compiler. Anyway, in sorting out a minimal working example to post here I found that it wasn’t doing what I thought at all. So here it is …

   struct Foo {
       enum BAR { A, B, C, D, E };
       private: typedef BAR BAR;
   };

   int main(int argc, char* argv[]) {
       int x = (Foo::BAR)42;
       int y = Foo::D;
  }

What seems to be happening, and what I was quite pleased about, is that, Foo takes on the enum constants after which BAR is made private. So I get no error on int y = but I get a Foo::BAR is private error at int x=. However this seems to only work with 5 or more constants in the enum, remove that E and it all compiles fine, i.e. BAR remains public.

What’s at work here? Thanks.

(PS. Compiler is GCC 4.4.3)

  • 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-15T16:21:14+00:00Added an answer on May 15, 2026 at 4:21 pm

    I can verify your results about four vs. five enum elements… this looks like an obscure GCC bug.

    As for “what’s at work here”, it’s due to a technicality between different symbol namespaces in C++ (inherited from C). See this answer for more details.

    If you don’t want a symbol named BAR to be exposed, simply omit the tag name:

    struct Foo {
        enum { A, B, C, D, E };
    };
    

    If you declare a named public enum, then no matter what you do, even if you hide the name using a typedef, outsiders can access the enum using an elaborated type specifier. Trying to hide a symbol name in this way is fruitless:

     struct Foo {
         enum BAR { A, B, C, D, E };
         private: typedef BAR BAR;
     };
    
     int main(int argc, char* argv[]) {
         int x = (enum Foo::BAR)42;  // works great!
         int y = Foo::D;
    }
    

    Is there any particular reason you want the constants to be public while the enum name remains private?

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

Sidebar

Related Questions

Sorry for a long question and not a very descriptive title, but my problem
Firstly, sorry about the question title, it may not be very descriptive for my
Sorry the title is not very clear. This is a follow up to my
Sorry this is not a very well defined question, I am thinking about an
Sorry, I know this is a very lame question to ask and not of
Sorry the title is not very descriptive but it is a tricky problem to
I'm sorry if this is a novice question, but I'm not very knowledgeable when
Sorry about the title. I do realize it's not very descriptive. :| Here is
Sorry if the title is not very clear. I was not sure about the
Sorry if the title was misleading or not very descriptive, but here is what

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.