C++ Nested Class

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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.

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

The expected output of the program would be as follows.

1
2
3
4
$ ./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.

Author

Lei Mao

Posted on

03-14-2020

Updated on

03-14-2020

Licensed under


Comments