8.15 — Nested types in classes

Consider the following short program:

There’s nothing wrong with this program. But because enum FruitType is meant to be used in conjunction with the Fruit class, it’s a little weird to have it exist independently from the class itself.

Nesting types

Unlike functions, which can’t be nested inside each other, in C++, types can be defined (nested) inside of a class. To do this, you simply define the type inside the class, under the appropriate access specifier.

Here’s the same program as above, with FruitType defined inside the class:

First, note that FruitType is now defined inside the class. Second, note that we’ve defined it under the public access specifier, so the type definition can be accessed from outside the class.

Classes essentially act as a namespace for any nested types. In the prior example, we were able to access enumerator APPLE directly, because the APPLE enumerator was placed into the global scope (we could have prevented this by using an enum class instead of an enum, in which case we’d have accessed APPLE via FruitType::APPLE instead). Now, because FruitType is considered to be part of the class, we access the APPLE enumerator by prefixing it with the class name: Fruit::APPLE.

Note that because enum classes also act like namespaces, if we’d nested FruitType inside Fruit as an enum class instead of an enum, we’d access the APPLE enumerator via Fruit::FruitType::APPLE.

Other types can be nested too

Although enumerations are probably the most common type that is nested inside a class, C++ will let you define other types within a class, such as typedefs, type aliases, and even other classes!

Like any normal member of a class, nested classes have the same access to members of the enclosing class that the enclosing class does. However, the nested class does not have any special access to the “this” pointer of the enclosing class.

Defining nested classes isn’t very common, but the C++ standard library does do so in some cases, such as with iterator classes. We’ll cover iterators in a future lesson.

8.x -- Chapter 8 comprehensive quiz
8.14 -- Anonymous objects

4 comments to 8.15 — Nested types in classes

  • James

    Good job Alex, but here am wondering how none static const private variable of the class Fruit "int m_percentageEaten = 0;" get to be initalized directly inside the class while you thought me in the previous lesson that only static const int and enum variable can be initialized inside the class that way. please Teacher tell me how this work.

    • Alex

      Initialization for static and non-static members works differently. Static members can only be initialized in the class if they are ints or enums. Non-static members can be initialized inside the class regardless of the type.

  • john

    Hi Alex! In the lesson on enumerators you mentioned we should prefer enum classes over enumerators if we have a C++11 compatible compiler since they are strongly typed and strongly scoped. What is the preferred way of using enumerations inside the class (using enum class complicates the access and we already limit the scope by having it inside the class)?

    • Alex

      Personally, I tend to use normal enumerations inside classes, since the double-namespacing from the class and the enum class seems overkill. But it’s really up to you. Enum classes have some additional advantages (e.g. you don’t have to worry about enumerator naming collisions if you have multiple nested enum classes).

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter