Nested Local Classes

It is possible to nest a class inside a local class. In this case, the nested class definition can appear outside the local-class body. However, the nested class must be defined in the same scope as that in which the local class is defined.

void foo(){    class Bar {    public:        // ...        class Nested;     // declares class Nested   };   // definition of Nested   class Bar::Nested {       // ...   };}

As usual, when we define a member outside a class, we must indicate the scope of the name. Hence, we defined Bar::Nested, which says that Nested is a class defined in the scope of Bar.

A class nested in a local class is itself a local class, with all the attendant restrictions. All members of the nested class must ...

Get C++ Primer, Fifth Edition 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.