【发布时间】:2013-01-01 22:49:44
【问题描述】:
我的理解是,如果使用 class 说明符,friend 声明也可以用作类的前向声明,如下例所示:
class A
{
friend class B;
B* b;
};
class B {};
int main() {}
但是,g++(4.6.3 和 4.7.0)给了我以下错误(g++-4.7 应该支持extended friend declarations),这是没有前向声明的预期:
main.cpp:6:2: 错误:‘B’没有命名类型
为了确认我对 friend class B; 应该作为前向声明的期望,我找到了 this answer 和 this answer,但两者都不是决定性的(或者我至少无法从它们中得出很多结论)所以我试图查阅 c++11 标准并找到了这个例子:
class X2 {
friend Ct; // OK: class C is a friend
friend D; // error: no type-name D in scope
friend class D; // OK: elaborated-type-specifier declares new class
}
根据我对第三个声明的阅读,我的friend class B 应该是一个声明一个新类的详细类型说明符。
我刚刚开始理解官方标准措辞,所以我一定遗漏了一些东西。我误会了什么?
【问题讨论】: