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” section 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:
However, the resolution for defect report 45 (to the standard) states the opposite, and hence defines the behavior you see:
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.