我有以下代码,其中类A将类B声明为朋友.在B类中声明的C类是否能够查看A类的私有声明/成员?
它与CL版本16(Visual Studio 2010)一起编译时没有错误,但是gcc g ++版本4.1.1给出了错误"typedef int A :: T在此上下文中是私有的".
函数调用为typedef时会出现相同的行为(这就是我发现差异的方式).
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
};
};
Run Code Online (Sandbox Code Playgroud)
我已经简要地看过了,但是找不到合适的搜索词.我还没读完标准.以前有关于这个主题的问题,或者在C++ FAQ中提到过吗?如果有的话,标准会破坏哪种行为?
来自标准文档., $11.4.2
将类声明为朋友意味着可以在友好类的基本说明符和成员声明中访问授予友谊的类中的私有成员和受保护成员的名称 .
标准文档的一个例子,他们自己,
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
};
};
Run Code Online (Sandbox Code Playgroud)
因此它应该没有任何错误.
归档时间: |
|
查看次数: |
1237 次 |
最近记录: |