Nested Classes
Although you may use public nested classes when they are part of an interface, consider a namespace to keep declarations out of the global scope.Definition:
A class can define another class within it; this is also
called a member class.
class Foo {
private:
// Bar is a member class, nested within Foo.
class Bar {
...
};
};
Pros:
This is useful when the nested (or member) class is only used
by the enclosing class; making it a member puts it in the
enclosing class scope rather than polluting the outer scope
with the class name. Nested classes can be forward declared
within the enclosing class and then defined in the
.cc
file to avoid including the nested class
definition in the enclosing class declaration, since the
nested class definition is usually only relevant to the
implementation.
Cons:
Nested classes can be forward-declared only within the
definition of the enclosing class. Thus, any header file
manipulating a Foo::Bar*
pointer will have to
include the full class declaration for Foo
.
Decision: Do not make nested classes public unless they are actually part of the interface, e.g., a class that holds a set of options for some method.