Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS

Introduction

In C++, the only nested function allowed is lambda expression, instead of a regular function. However, C++ does allow nested regular classes. The users don’t actually write too many nested classes in most of the applications. The nested classes have been widely used in C++ STL standard library, such as Iterators.


In this blog post, I would like to show some examples of defining a nested class inside a class.

Examples

In this example, we have implemented a Enclose class which has two nested classes, a private PrivateNested class and a public PublicNested class. The type of the PublicNested class could be accessed by Enclose::PublicNested outside the Enclose class scope. The type of the PrivateNested class could not be accessed outside the Enclose class scope since it is private. However, we could still create an instance of it using the public method getPrivateNestedInstance and deduce its type using auto.

/*
 * nested_class.cpp
 */
#include <iostream>

class Enclose
{
private:
    // The declaration of the nested class has to go before the methods related to it.
    class PrivateNested;
    // PublicNested mPublicNestedInstance; // incomplete type is not allowed
    // PrivateNested mPrivateNestedInstance; // incomplete type is not allowed
public:
    Enclose();
    // The declaration of the nested class has to go before the methods related to it.
    class PublicNested;
    PublicNested getPublicNestedInstance(int x);
    PrivateNested getPrivateNestedInstance(int x);
};

Enclose::Enclose()
{
}

class Enclose::PublicNested
{
public:
    PublicNested(int x);
    int getValue();
private:
    int mX;
};

Enclose::PublicNested::PublicNested(int x) : mX{x}
{
}

int Enclose::PublicNested::getValue()
{
    return this->mX;
}

Enclose::PublicNested Enclose::getPublicNestedInstance(int x)
{
    return PublicNested{x};
}

class Enclose::PrivateNested
{
public:
    PrivateNested(int x);
    int getValue();
private:
    int mX;
};

Enclose::PrivateNested::PrivateNested(int x) : mX{x}
{
}

int Enclose::PrivateNested::getValue()
{
    return this->mX;
}

Enclose::PrivateNested Enclose::getPrivateNestedInstance(int x)
{
    return PrivateNested{x};
}

int main()
{
    Enclose encloseInstance{};
    Enclose::PublicNested publicNestedInstance1 = encloseInstance.getPublicNestedInstance(1);
    std::cout << publicNestedInstance1.getValue() << std::endl;
    Enclose::PublicNested publicNestedInstance2{2};
    std::cout << publicNestedInstance2.getValue() << std::endl;
    // Enclose::PrivateNested privateNestedInstance1 = encloseInstance.getPrivateNestedInstance(1); // Enclose::PrivateNested is not accessible
    auto privateNestedInstance2 = encloseInstance.getPrivateNestedInstance(2);
    std::cout << publicNestedInstance2.getValue() << std::endl;
}

To compile the program, please run the following command in the terminal.

$ g++ nested_class.cpp -o nested_class --std=c++11

The expected output of the program would be as follows.

$ ./nested_class 
1
2
2

Conclusions

C++ nested class is not hard to implement. We would see its practical usage when we discuss C++ Iterators.