以下内容是否相当于前向声明?
This is related to a recent question.
Basically the following code:
class A
{
class B* b;
B* c;
};
compiles although class B
is not declared or forward-declared. Is this syntax equivalent to a forward declaration? Are there any differences?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在同一个声明中声明类型和对象。
声明一个类型
B
和一个对象b
,该对象具有指向B
的类型指针。该类型是不完整的,并且在它出现的范围中查找,如果查找未能找到该类的现有声明,则该类型在最近的封闭命名空间范围中命名一个类型(严格非类非函数原型)范围,通常是命名空间)。该对象是声明所在范围的成员(在本例中为class A
)。在大多数情况下,更常见的是一起声明完整类型和对象,在这种情况下,类型有时会保持匿名。例如,
名称范围规则标准的相关部分是 7.1.5.3 [dcl.type.elab] 详细类型说明符 / 2 以及 3.4.4 和 3.3.1 中引用的部分:
You can declare a type and an object in the same declaration.
Declares a type,
B
and an objectb
which has type pointer toB
. The type is incomplete and is looked up in the scope in which it occurs, if the lookup fails to find an existing declaration for the class then the type names a type in the nearest enclosing namespace scope (strictly non-class non-function-prototype scope, which is usually a namespace). The object is a member of the scope in which the declaration appears (in this case,class A
).In most cases it's more common to declare a complete type and an object together, in this case the type is sometimes left anonymous. E.g.
The relevant parts of the standard for the name scoping rules are 7.1.5.3 [dcl.type.elab] Elaborated type specifiers / 2 and the referenced sections in 3.4.4 and 3.3.1 :
不,它是一个指向 B 的指针的声明。您在这里不是声明 B,只是指向它的指针,并且没有任何关于它的内容。
编辑:我错了,抱歉。参见其他答案。
No, it's a declaration, of a pointer to B. You are not declaring B here, only a pointer to it, and there's nothing forward about it.
Edit: I was wrong, sorry. See other answer.
我想添加一些细节来回答Charles Bailey:
是的,它定义了不完整的类型
B< /code> 和
b
同时作为指向B
的指针。但有趣的是:“嵌套类声明的范围可见性的一个例外是当类型名称与前向声明一起声明时。在这种情况下,前向声明声明的类名在封闭类的外部是可见的,其范围定义为最小的封闭非类范围。” (嵌套类声明)
这意味着类型
B
定义在A
范围之外,它允许您定义变量globalB
。如果您不希望在A
范围之外定义B
,则可以使用{}
,就像与类型一起使用一样在我的示例中为 >C
。顺便说一下,这个例子是正确的,它的输出是:
1
I would like to add few details to answer of Charles Bailey:
Yes, it defines incomplete type
B
andb
as a pointer toB
at once. But here comes the fun:"An exception to the scope visibility of a nested class declaration is when a type name is declared together with a forward declaration. In this case, the class name declared by the forward declaration is visible outside the enclosing class, with its scope defined to be the smallest enclosing non-class scope." (Nested Class Declarations)
Which means that type
B
is defined out of scopeA
which allows you to define variableglobalB
. If you don't wantB
to be defined out of scopeA
, you can use{}
just like it is used with typeC
in my example.By the way, this example is correct and its output is:
1