为什么 C++需要类成员的前向声明吗?
我的印象是 C++ 中的所有内容都必须在使用之前声明。
事实上,我记得读到过,这就是为什么在返回类型中使用 auto
在没有 decltype
之类的情况下是无效的 C++0x 的原因 >:编译器在计算函数体之前必须知道声明的类型。
想象一下当我注意到(很长一段时间后)以下代码实际上完全合法时我的惊讶:
[编辑:更改了示例。]
class Foo
{
Foo(int x = y);
static const int y = 5;
};
所以现在我不明白:
为什么编译器不这样做当在其他地方需要它们时,需要在类内部进行前向声明吗?
I was under the impression that everything in C++ must be declared before being used.
In fact, I remember reading that this is the reason why the use of auto
in return types is not valid C++0x without something like decltype
: the compiler must know the declared type before evaluating the function body.
Imagine my surprise when I noticed (after a long time) that the following code is in fact perfectly legal:
[Edit: Changed example.]
class Foo
{
Foo(int x = y);
static const int y = 5;
};
So now I don't understand:
Why doesn't the compiler require a forward declaration inside classes, when it requires them in other places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该标准规定(第 3.3.7 节):
这可能是通过将内联成员函数的处理主体延迟到解析整个类定义之后来完成的。
The standard says (section 3.3.7):
This is probably accomplished by delaying processing bodies of inline member functions until after parsing the entire class definition.
类体内的函数定义被视为在类定义之后实际定义。所以你的代码相当于:
Function definitions within the class body are treated as if they were actually defined after the class has been defined. So your code is equivalent to:
其实我觉得你需要把问题倒过来才能理解。
为什么 C++ 需要前向声明?
由于 C++ 的工作方式(包括文件,而不是模块),否则它需要等待整个翻译单元才能评估,当然,什么函数是。这里有几个缺点:
为什么类不同?
根据定义,类是包含的。这是一个小单位(或者应该是......)。因此:
,因此我们可以避免这种烦人的类前向声明规则。
Actually, I think you need to reverse the question to understand it.
Why does C++ require forward declaration ?
Because of the way C++ works (include files, not modules), it would otherwise need to wait for the whole Translation Unit before being able to assess, for sure, what the functions are. There are several downsides here:
Why is a class different ?
A class is by definition contained. It's a small unit (or should be...). Therefore:
Therefore we can eschew this annoying forward-declaration rule for classes.
只是猜测:编译器保存函数体,并且在类声明完成之前不会实际处理它。
Just guessing: the compiler saves the body of the function and doesn't actually process it until the class declaration is complete.
与命名空间不同,类的作用域不能重新打开。它是绑定的。
想象一下,如果所有内容都需要提前声明,那么在标头中实现一个类。我认为由于它是绑定的,因此按原样编写语言更符合逻辑,而不是要求用户在类中向前编写(或要求定义与声明分开)。
unlike a namespace, a class' scope cannot be reopened. it is bound.
imagine implementing a class in a header if everything needed to be declared in advance. i presume that since it is bound, it was more logical to write the language as it is, rather than requiring the user to write forwards in the class (or requiring definitions separate from declarations).