The Standard text is very clear about this:
The C++ Standard (2003) says in $11.8/1 [class.access.nest],
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.
Now, regarding the second case in your question, the print()
function is a friend of A
, that means, it can access private members of A
only, not that of B
, since print()
function is NOT a friend of B
. And making print()
friend of A
does NOT automatically make it friend
of B
also.
By the way, the Standard quotation has one defect. It says the nested classes don't have access to private members of the enclosing class. But in C++0x, it has been corrected: in C++0x, nested classes do have access to private members of the enclosing class (though the enclosing class still doesn't have access to private members of the nested classes).
See this Defect Report :
Somewhat related topic: