本文介绍了朋友声明不转发声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,如果 class 说明符, friend 声明也可以用作类的前向声明如下例所示:

My understanding was that a friend declaration could also serve as a forward declaration for a class if the class specifier was used, as in this example:

class A
{
    friend class B;
    B* b;
};

class B {};

int main() {}

但是,g ++(4.6.3和4.7.0)给了我以下错误(g ++-4.7应该支持),而无需使用前向声明:

However, g++ (4.6.3 and 4.7.0) gives me the following error (g++-4.7 should have support for extended friend declarations), which is expected without a forward declaration:

试图确认我对朋友B级的期望; 应该用作前向声明,我发现和,但是都不是结论性的(或者至少不能从他们那里得出很多结论),因此我尝试参考c ++ 11标准并找到了以下示例:

In an attempt to confirm my expectations that the friend class B; should serve as a forward declaration, I found this answer and this answer, but neither was conclusive (or I couldn't conclude much from them at least) so I attempted to consult the c++11 standard and found this example:

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
}

根据我的阅读在第三个声明中,我的朋友类B 应该是声明新类的修饰类型说明符

Based on my reading of the the third declaration, my friend class B should be an elaborated-type-specifier declaring a new class.

我才刚刚开始理解官方的标准措辞,所以我肯定缺少一些东西。我误解了什么?

I am just starting to understand official standard wording, so I must be missing something. What am I misunderstanding?

推荐答案

您的朋友B级; 声明确实用作前向声明,但是在提供匹配的声明之前,名称查找不会找到这种声明。

Your friend class B; declaration does serve as a forward declaration, but such declaration is not found by name lookup until a matching declaration is provided.

[class。朋友] / 11

这篇关于朋友声明不转发声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:06