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 (§ 6.1.1, p. 205), 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    struct Bar {        Loc locVal; // ok: uses a 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 a global object ...

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.