Nested classes

You can define a class within a class. If the nested class is declared as public, then you can create objects in the container class and return them to external code. Typically, however, you will want to declare a class that is used by the class and should be private. The following declares a public nested class:

    class outer     {     public:         class inner          {         public:             void f();         };          inner g() { return inner(); }     };      void outer::inner::f()     {          // do something     }

Notice how the name of the nested class is prefixed with the name of the containing class.

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.