What is a nested class? Why can it be useful?
A nested class is a class enclosed within the scope of another class. For example:
// Example 1: Nested class
//
class OuterClass
{
class NestedClass
{
// …
};
// …
};
Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if Nested Class is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if Nested Class is private, then only OuterClass’s members and friends can use Nested Class. When you instantiate as outer class, it won’t instantiate inside class.