Nested Classes
There are several side effects to classes nested inside classes, which I usually see as flaws (if not pure antipatterns).
Imagine the following code:
class A { public : class B { } ;
Or even:
class A { public : class B ;
So:
- Privileged Access: A :: B has privileged access to all members of A (methods, variables, characters, etc.), which weakens encapsulation
- The area is a candidate for finding a character: the code inside B will see the characters all from A as possible candidates for finding a character, which may confuse the code
- forward-declaration: You cannot forward A :: B without giving a full declaration to A
- Extensibility: It is not possible to add another class A :: C unless you own A
- Verbose code: placing classes in classes only increases the size of the headers. You can still split this into multiple declarations, but there is no way to use aliases, import, or use names like a namespace.
As a conclusion, if there are exceptions (for example, a nested class is an intimate part of a nesting class ... And even then ...), I donโt see the point in nested classes in normal code, since the disadvantages of the advantages are perceived advantages.
Also, it smells like a clumsy attempt to mimic a namespace without using C ++ namespaces.
On the pro side, you isolate this code and, if it is closed, make it unusable, but from the class "outside" ...
Enclosed Transfers
Pros: everything.
Con: Nothing.
The fact that listing elements 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, placing each enumeration in a different namespace / class will allow you to avoid this collision:
namespace Value { enum type { empty = 7, undefined, defined } ; } namespace Glass { enum type { empty = 42, half, full } ; }
Note that C ++ 0x defines a class enumeration:
enum class Value { empty, undefined, defined } ; enum class Glass { empty, half, full } ;
exactly for this kind of problem.