Nested classes: Access to protected member of enclosing class from a nested protected class
This code compiles on msvc/g++:
class A{
protected:
int i;
class B{
public:
A* a;
B(A* a_)
:a(a_){
}
void doSomething(){
if (a)
a->i = 0;//<---- this part
}
};
public:
A()
:i(0){
}
};
As you can see, B gets access to "protected" sectio开发者_如何学Cn of enclosing class, although it isn't declared as friend.
Is this a standard(standard-conforming) behavior?
I use this feature sometimes, but I don't remember a rule saying that nested protected class should automatically get access to all protected data of enclosing class.
In the C++03 standard, 11.8p1 says:
The members of a nested class have no special access to members of an enclosing class.
However, the resolution for defect report 45 (to the standard) states the opposite, and hence defines the behavior you see:
A nested class is a member and as such has the same access rights as any other member.
In C++0x the text of 11.8 has been changed to reflect this fact, so this is valid behavior for both C++03 and C++0x conforming compilers.
See also this thread from the cprogramming.com forums.
I don't have a copy of C++03 handy but from a draft (n3090) of C++0x:
11.8 Nested classes
1 A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.
[ Example:
class E {
int x;
class B { };
class I {
B b; // OK: E::I can access E::B
int y;
void f(E* p, int i) {
p->x = i; // OK: E::I can access E::x
}
};
int g(I* p) {
return p->y; // error: I::y is private
}
};
So at least in the next standard, enclosed classes can access the outer class' members as any normal member function would.
Update: This isn't allowed in the current standard. But a defect report (DR 45) was filed to fix this. (SigTerm's edit took this one out. Please, be careful.)
Update #2: I tried with VS2010, g++ (4.0.1 Apple) with -Wall -ansi -pedantic -std=c++98
and Comeau (4.3.10.1) in strict C++03 mode with C++0x extensions disabled -- all of them seem to accept access to the outer class private
members in the inner class members.
Refer $9.7/1.
"The nested class is in the scope of its enclosing class. Except by using explicit pointers, references, and object names, declarations in a nested class can use only type names, static members, and enumerators from the enclosing class."
Is this a standard(standard-conforming) behavior?
No.
According to C++-2003
Section 11.8.1
Nested classes
The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.
[Example:
class E {
int x;
class B { };
class I {
B b; //error: E::B is private
int y;
void f(E* p, int i)
{
p->x = i; //error: E::x is private
}
};
int g(I* p)
{
return p->y; //error: I::y is private
}
};
—end example]
But there is a slight modification to that section in ISO/IEC N 3092
which says
A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.
精彩评论