Team LiB
Previous Section Next Section

18.6. Local Classes

18.6. 局部类

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.

类似地,不允许局部类声明 static 数据成员,没有办法定义它们。

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:

局部类可以访问的外围作用域中的名字是有限的。局部类只能访问在外围作用域中定义的类型名、static 变量(第 7.5.2 节)和枚举成员,不能使用定义该类的函数中的变量:

     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.

实际上,局部类中 private 成员几乎是不必要的,通常局部类的所有成员都为 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.

嵌套在局部类中的类本身是一个带有所有附加限制的局部类。嵌套类的所有成员必须在嵌套类本身定义体内部定义。

Team LiB
Previous Section Next Section