Access to the inner class

Can a class written in a method of another class (inner class) access method variables? I mean the code below:

class A { void methodA( int a ) { class B { void processA() { a++; } }; std::cout<<"Does this program compile?"; std::cout<<"Does the value of the variable 'a' increments?"; }; 

};

Is it legal? Does the meaning of a know? Please suggest how.

Thanks, Pavan.

+4
source share
2 answers

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.

+4
source

The short answer is no. Local classes in C ++ do not have access to their included functional variables (with a few caveats). You can learn more about local C ++ classes here , and also see this good SO answer . For emphasis:

A local class is declared in a function definition. Declarations in the local class can only use type names, enumerations, static variables from the scope, and also external variables and functions.

 int x; // global variable void f() // function definition { static int y; // static variable y can be used by // local class int x; // auto variable x cannot be used by // local class extern int g(); // extern function g can be used by // local class class local // local class { int g() { return x; } // error, local variable x // cannot be used by g int h() { return y; } // valid,static variable y int k() { return ::x; } // valid, global x int l() { return g(); } // valid, extern function g }; } int main() { local* z; // error: the class local is not visible // ...} 
+1
source

All Articles