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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:41:34+00:00 2026-05-10T17:41:34+00:00

What are the pros and cons of using nested public C++ classes and enumerations?

  • 0

What are the pros and cons of using nested public C++ classes and enumerations? For example, suppose you have a class called printer, and this class also stores information on output trays, you could have:

class printer { public:     std::string name_;      enum TYPE     {         TYPE_LOCAL,         TYPE_NETWORK,     };      class output_tray     {         ...     };     ... };  printer prn; printer::TYPE type; printer::output_tray tray; 

Alternatively:

class printer { public:     std::string name_;     ... };  enum PRINTER_TYPE {     PRINTER_TYPE_LOCAL,     PRINTER_TYPE_NETWORK, };  class output_tray {     ... };  printer prn; PRINTER_TYPE type; output_tray tray; 

I can see the benefits of nesting private enums/classes, but when it comes to public ones, the office is split – it seems to be more of a style choice.

So, which do you prefer and why?

  • 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-10T17:41:35+00:00Added an answer on May 10, 2026 at 5:41 pm

    Nested classes

    There are several side effects to classes nested inside classes that I usually consider flaws (if not pure antipatterns).

    Let’s imagine the following code :

    class A {    public :       class B { /* etc. */ } ;     // etc. } ; 

    Or even:

    class A {    public :       class B ;     // etc. } ;  class A::B {    public :     // etc. } ; 

    So:

    • Privilegied Access: A::B has privilegied access to all members of A (methods, variables, symbols, etc.), which weakens encapsulation
    • A’s scope is candidate for symbol lookup: code from inside B will see all symbols from A as possible candidates for a symbol lookup, which can confuse the code
    • forward-declaration: There is no way to forward-declare A::B without giving a full declaration of A
    • Extensibility: It is impossible to add another class A::C unless you are owner of A
    • Code verbosity: putting classes into classes only makes headers larger. You can still separate this into multiple declarations, but there’s no way to use namespace-like aliases, imports or usings.

    As a conclusion, unless exceptions (e.g. the nested class is an intimate part of the nesting class… And even then…), I see no point in nested classes in normal code, as the flaws outweights by magnitudes the perceived advantages.

    Furthermore, it smells as a clumsy attempt to simulate namespacing without using C++ namespaces.

    On the pro-side, you isolate this code, and if private, make it unusable but from the "outside" class…

    Nested enums

    Pros: Everything.

    Con: Nothing.

    The fact is enum items will pollute the global scope:

    // collision enum Value { empty = 7, undefined, defined } ; enum Glass { empty = 42, half, full } ;  // empty is from Value or Glass? 

    Ony by putting each enum in a different namespace/class will enable you to avoid this collision:

    namespace Value { enum type { empty = 7, undefined, defined } ; } namespace Glass { enum type { empty = 42, half, full } ; }  // Value::type e = Value::empty ; // Glass::type f = Glass::empty ; 

    Note that C++0x defined the class enum:

    enum class Value { empty, undefined, defined } ; enum class Glass { empty, half, full } ;  // Value e = Value::empty ; // Glass f = Glass::empty ; 

    exactly for this kind of problems.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This answer is to why "initializer element is not constant".… May 12, 2026 at 1:15 pm
  • Editorial Team
    Editorial Team added an answer Actually, these both are exactly the same: def foo(*args): pass… May 12, 2026 at 1:15 pm
  • Editorial Team
    Editorial Team added an answer OnResize is declared as a protected property of TControl. You… May 12, 2026 at 1:15 pm

Related Questions

What are the pros and cons of using table aliases in SQL? I personally
What are the pros and cons of using a hosting provider for a Subversion
What are the pros and cons of using Criteria or HQL ? The Criteria
What are the pros and cons of using System.Security.Cryptography.RNGCryptoServiceProvider vs System.Random . I know

Trending Tags

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

Top Members

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.