Section 18.6. Local Classes


18.6. Local Classes

A class can be defined inside a function body. Such a class is called a local class. A local class defines a type that is visible only in the local scope in which it is defined. Unlike nested classes, the members of a local class are severely restricted.

All members, including functions, of a local class must be completely defined inside the class body. As a result, local classes are much less useful than nested classes.



In practice, the requirement that members be fully defined within the class limits the complexity of the member functions of a local class. Functions in local classes are rarely more than a few lines of code. Beyond that, the code becomes difficult for the reader to understand.

Similarly, a local class is not permitted to declare static data members, there being no way to define them.

Local Classes May Not Use Variables from the Function's Scope

The names from the enclosing scope that a local class can access are limited. A local class can access only type names, static variables (Section 7.5.2, p. 255), and enumerators defined within the enclosing local scopes. A local class may not use the ordinary local variables of the function in which the class is defined:

      int a, val;      void foo(int val)      {         static int si;         enum Loc { a = 1024, b };         // Bar is local to foo         class Bar {         public:             Loc locVal; // ok: uses local type name             int barVal;             void fooBar(Loc l = a)         // ok: default argument is Loc::a             {                barVal = val;      // error: val is local to foo                barVal = ::val;    // ok: uses global object                barVal = si;       // ok: uses static local object                locVal = b;        // ok: uses enumerator             }         };         // ...      } 

Normal Protection Rules Apply to Local Classes

The enclosing function has no special access privileges to the private members of the local class. Of course, the local class could make the enclosing function a friend.

In practice, private members are hardly ever necessary in a local class. Often all members of a local class are public.



The portion of a program that can access a local class is very limited. A local class is encapsulated within its local scope. Further encapsulation through information hiding is often overkill.

Name Lookup within a Local Class

Name lookup within the body of a local class happens in the same manner as for other classes. Names used in the declarations of the members of the class must be in scope before the use of the name. Names used in definitions of the members can appear anywhere in the scope of the local class. Names not resolved to class members are searched first in the enclosing local scope and then out to the scope enclosing the function itself.

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 local scope as that in which the local class is defined. As usual, the name of the nested class must be qualified by the name of the enclosing class and a declaration of the nested class must appear in the definition of the local class:

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

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



C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net