Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A nested class in C++ is a class declared entirely within the lexical scope of another class. It acts as a member of the enclosing class and is subject to standard access specifiers (public, protected, private), which dictate its visibility to the global scope or other classes.
class EnclosingClass {
public:
    class NestedClass {
        // Nested class members
    };
};

Scope and Instantiation

The nested class exists within the class scope of the enclosing class. To instantiate a publicly accessible nested class from outside the enclosing class, you must use the scope resolution operator (::).
EnclosingClass::NestedClass nestedObject;
If the nested class is declared as private or protected, it cannot be instantiated or accessed from outside the enclosing class, restricting its availability strictly to the enclosing class’s internal implementation.

Access Rules

The relationship between the enclosing class and the nested class is governed by specific access control rules regarding each other’s members. 1. Nested Class Accessing Enclosing Class Because a nested class is a member of the enclosing class, it inherently possesses the same access rights as any other member (a rule formalized in C++03 via Defect Report 45). It has full access to all private, protected, and public members, types, and enumerations of the enclosing class.
  • Static members: The nested class can access static members of the enclosing class directly.
  • Non-static members: The nested class must be provided with an instance (object pointer or reference) of the enclosing class to access its non-static members, as the nested class does not possess an implicit this pointer to the enclosing object.
2. Enclosing Class Accessing Nested Class The enclosing class does not possess special access privileges to the nested class. It cannot access the private or protected members of the nested class. To grant the enclosing class access to these members, the enclosing class must be explicitly declared as a friend inside the nested class.
class Enclosing {
private:
    int non_static_val;
    static int static_val;

public:
    class Nested {
    private:
        int nested_val;
        
        // Explicitly granting access to the enclosing class
        friend class Enclosing; 

    public:
        void modifyEnclosing(Enclosing& instance) {
            // Requires an instance to access non-static private member
            instance.non_static_val = 10; 
            
            // Direct access to static private member
            static_val = 20; 
        }
    };

    void modifyNested(Nested& instance) {
        // Valid only because Enclosing is a friend of Nested
        instance.nested_val = 30; 
    }
};

int Enclosing::static_val = 0;

Out-of-Class Definition

A nested class can be forward-declared within the enclosing class and defined outside of it. When defining the nested class outside, the scope resolution operator must be used to associate the definition with the enclosing class.
class Enclosing {
public:
    class Nested; // Forward declaration
};

// Out-of-class definition
class Enclosing::Nested {
public:
    Nested() {
        // Constructor implementation
    }
};
Similarly, member functions of the nested class can be declared inside the nested class and defined outside both classes by chaining the scope resolution operators.
class Enclosing {
public:
    class Nested {
    public:
        void nestedMethod();
    };
};

// Out-of-class member function definition
void Enclosing::Nested::nestedMethod() {
    // Method implementation
}
Master C++ with Deep Grasping Methodology!Learn More