Should I use nested classes?

Should I use nested classes?

Nested Class can be used whenever you want to create more than once instance of the class or whenever you want to make that type more available. Nested Class increases the encapsulations as well as it will lead to more readable and maintainable code.

Can we create a nested class?

Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass , a nested class can be declared private , public , protected , or package private. (Recall that outer classes can only be declared public or package private.)

Why are nested classes static?

Static Nested Classes As with static members, these belong to their enclosing class, and not to an instance of the class. They can have all types of access modifiers in their declaration. They only have access to static members in the enclosing class. They can define both static and non-static members.

What are the pros and cons of nested classes?

One con that I could see in doing nested classes is you end up with a really large source file that could be hard to grok when you are searching for a section. There are no pros and cons per se of using nested public C++ classes. There are only facts. Those facts are mandated by the C++ standard.

Is it possible to add another class to a nested class?

Another fact about nested classes is, that it is impossible to add another class A::C as a nested class of A unless you are owner of A. However, this is perfectly reasonable, because nested classes have privileged access.

Why are multiple classes using the same enum?

If multiple classes are utilizing an enum defined within a different class, then those classes are directly dependent on that other concrete class (that owns the enum). That surely represents a design flaw since that class will be responsible for that enum as well as other responsibilities.