Can a class written in a method of another class(inner class), access the methods variables? I mean in 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 this legal?Does, the value of 'a' increments? Please suggest how.
Thanks, Pavan.
No it is not legal
class B
is a Local class to methodA()
.
class B
cannot access nonstatic "automatic" local variables of the enclosing function. But it can access the static variables from the enclosing scope.
There are several restrictions on what local classes can have access to.
Here is an reference from the C++ Standard:
9.8 Local class declarations [class.local]
- A class can be defined within a function definition; such a class is called a local class. The name of a local class is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing 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. An enclosing function has no special access to members of the local class; it obeys the usual access rules (clause 11). Member functions of a local class shall be defined within their class definition, if they are defined at all.
3. If class X is a local class a nested class Y may be declared in class X and later defined in the definition of class X or be later defined in the same scope as the definition of class X. A class nested within a local class is a local class.
4. A local class shall not have static data members.
Short answer, no. Local classes in C++ do not have access to their enclosing function variables (with a few caveats). You can read more about C++ local classes here, and also see this nice SO answer. For emphasis:
A local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as 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
// ...}
精彩评论