No it's not legal
class B - Local class - methodA() .
class B cannot access non-static "automatic" local variables of a nested function. But it can access static variables from the scope.
There are several restrictions on access to local classes.
Here is a link to the C ++ standard:
9.8 Local class declarations [class.local]
- A class can be defined in a function definition; such a class is called a local class. The name of a local class is local to its scope. A local class is included in the scope of the enclosing scope and has the same access to names outside the function as the closing function. Declarations in the local class can only use type names, static variables, external variables, and functions and enumerations from the scope.
[Example:
int x; void f() { static int s ; int x; extern int g(); struct local { int g() { return x; } // error: x is auto int h() { return s; } // OK int k() { return ::x; } // OK int l() { return g(); } // OK }; // ... } local* p = 0; // error: local not in scope
-end example]
2. The closing function does not have special access to members of the local class; it is subject to normal access rules (section 11). The functions of members of a local class must be defined within their class definition, if they are defined at all.
3. If class X is a local class, a nested class Y can be declared in class X and later defined in the definition of class X or later defined in the same scope as the definition of class X. A class nested in a local class is a local class.
4. The local class must not contain static data.
source share