Local Classes


Classes may be declared within the body of a function. Classes declared in this way are referred to as local classes, and their scope is that of the containing function. A local class may refer only to:

from its enclosing scope. The following applies to local classes.

Example

int X=0;                     // global variable X
int g();                     // a function returning integer
 
int f()
{
 int X=0;                    // an automatic variable X
 static int s=0;             // a static variable

 struct local                // a local class (local to f that is)
 {
  int Test1() {return ::X;}  // Correct - refers to global X
  int Test2() {return X;}    // Syntax Error !! Local X is automatic
  int Test3() {return s;}    // Correct - refers to local static
  int Test4() {return g();}  // Correct to refer to function like this
 };
}
 
local test;                  // Syntax Error !! class local not known outside function f.