A nested class in C++ is a class defined inside another class. It lives in the scope of the enclosing class, so its full name is Outer::Inner, and it can be hidden with access control. Use a nested class when a helper type only makes sense together with its enclosing class, such as a node of a linked list or an iterator.
This guide covers the syntax, how access works in both directions, defining the nested class outside, and the typical use cases for a nested class in C++. It also separates nested classes from inheritance and from namespaces.
Define the inner class inside the outer class body. Where you place it (public, private or protected section) decides who can name it.
Node is private, so code outside LinkedList cannot create or even name it. The node layout is an implementation detail that can change without breaking callers.
Since C++11, a nested class is a member of the enclosing class, so it has the same access rights as any other member: it can use the private names of the outer class (through an object or pointer, since there is no implicit outer object).
The reverse is not true. The enclosing class has no special access to the private members of the nested class. If the outer class must read the inner one's private data, make them public, or declare the outer class a friend.
A nested class also does not hold an implicit pointer to an outer object, unlike an inner class in Java. If it needs one, store a reference or pointer yourself.
For readability you can declare the inner class in the outer class and define it later with the qualified name.
Names of types used in the definition are looked up in the scope of Report, so you can use other nested names without qualification inside it.
std::vector<int>::iterator is a nested type name, and a custom container often defines iterator and const_iterator inside itself.Parser::Options.Connection::State.| Need | Use |
|---|---|
| Helper type hidden from users | private nested class |
| Type used by many classes | separate class in a namespace |
| Reuse behaviour in a subtype | inheritance |
| Group unrelated names | namespace |
A nested class does not create an "is-a" or "has-a" relationship. It only affects naming and access. Defining many large nested classes in a header makes it long, so move big helpers to their own file when they grow.
Yes, as a member it does, but it needs an object or pointer of the outer type because there is no implicit outer instance.
No. It needs to be declared a friend of the nested class.
No. Defining a type inside a class does not add data. Only data members count towards sizeof.
The word is not used for classes. All nested classes in C++ behave like Java's static nested classes, with no implicit outer pointer.
Use the qualified name, such as LinkedList::Node, which is only allowed when the nested class is accessible.
A nested class is declared inside another class, uses the outer name as a scope and follows ordinary access control. Make helper types private, remember that the outer class gets no special access to the inner one, and use nested types for iterators, nodes, options and enumerations that belong to a single class.