I have the following code where class A declares class B as friend. Should class C, declared within class B, be able to view private declarations/members of class A?
It compiles without error with CL version 16 (Visual Studio 2010), but gcc g++ version 4.1.1 gives the error "typedef int A::T is private within this context".
The same behaviour occurs with functions calls as typedefs (which is how I discovered the difference).
开发者_运维百科class A {
friend class B;
typedef int T;
};
class B {
A::T t; // ok
typedef A::T U; // ok
class C {
U u; // ok
A::T v; // compile error on gcc
};
};
I have seearched briefly, but not been able to find the right search terms. I've yet to read through the standard. Are there any previous questions on the subject, or mentioned in the C++ FAQ? Which behaviour is imlpied by the standard, if either?
From standard docs., $11.4.2
Declaring a class to be a friend implies that the names of private and protected members from the class granting friendship can be accessed in the base-specifier s and member declarations of the befriended class.
An example from the standard docs., themselves,
class A {
class B { };
friend class X;
};
struct X : A::B { // OK: A::B accessible to friend
A::B mx; // OK: A::B accessible to member of friend
class Y {
A::B my; // OK: A::B accessible to nested member of friend
};
};
Hence it should work without any error.
There seems to be some defect in the original standard C++03
As per C++03 [pre CD1] your code should not compile because the wording and the example says that private members of a class(granting friendship) cannot be accessed in the nested member of the friend class.
C++11 gives the same example as in C++03. The only change made to that example is that the nested member(class) of the friend class is able to access the private member of the class granting friendship.
Declaring a class to be a friend implies that the names of private and protected members from the class granting friendship can be accessed in the base-specifiers and member declarations of the befriended class.
Also look at issue #45
Prasoon mentioned issue #45... this behavior changes in C++0x. The old behavior was (11.7 [class.access.nest]
paragraph 1):
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.
This clearly states that gcc 4.1 is correct according to the C++03 rules. gcc 4.5 and MSVC2010 are using the C++0x rules.
精彩评论