为什么前向声明有必要吗? [英] Why are forward declarations necessary?

查看:228
本文介绍了为什么前向声明有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  如果C ++消除头文件?

在如C#和​​Java语言也没有必要使用它之前声明(例如)一类。如果我理解正确的话,这是因为编译器对code两遍。首先它只是收集资料,并在第二个它会检查code是正确的。

In languages like C# and Java there is no need to declare (for example) a class before using it. If I understand it correctly this is because the compiler does two passes on the code. In the first it just "collects the information available" and in the second one it checks that the code is correct.

在C和C ++编译器只做一通让一切都需要提供在那个时候。

In C and C++ the compiler does only one pass so everything needs to be available at that time.

所以我的问题基本上都是为什么没有它做这样的C和C ++。那岂不是消除需求的头文件?

So my question basically is why isn't it done this way in C and C++. Wouldn't it eliminate the needs for header files?

推荐答案

简短的回答是,计算能力和资源,下定义的时间和Java的沿25年后进来的时候,之间指数先进的。

The short answer is that computing power and resources advanced exponentially between the time that C was defined and the time that Java came along 25 years later.

较长的答案...

编译单元的最大尺寸的 - code,在单块编译过程的块 - 将是由​​的内存量的限制,该编译计算机具有。为了处理您输入到机器code中的符号,编译器需要容纳所有的符号查找表,并引用他们,因为它在你的code遇到他们。

The maximum size of a compilation unit -- the block of code that a compiler processes in a single chunk -- is going to be limited by the amount of memory that the compiling computer has. In order to process the symbols that you type into machine code, the compiler needs to hold all the symbols in a lookup table and reference them as it comes across them in your code.

当C于1972年创建,计算资源是更加稀少,在高premium - 存储复杂程序的整个符号表一次简单所需的内存是不是在大多数系统中可用。固定存储也是昂贵,而且极为缓慢,所以喜欢虚拟内存或磁盘上的符号表的存储部分的想法根本不会允许在编制一个合理的时间框架。

When C was created in 1972, computing resources were much more scarce and at a high premium -- the memory required to store a complex program's entire symbolic table at once simply wasn't available in most systems. Fixed storage was also expensive, and extremely slow, so ideas like virtual memory or storing parts of the symbolic table on disk simply wouldn't have allowed compilation in a reasonable timeframe.

这个问题最好的解决办法是由具有人类理清这将在未来的时间,这编译单元需要的符号表的部分数据块的code成小块。有关声明的程序员什么,他的的使用保存的具有计算机搜索整个计划的任何程序员的巨大努力,堂堂一个相当小的任务的可能的使用。

The best solution to the problem was to chunk the code into smaller pieces by having a human sort out which portions of the symbol table would be needed in which compilation units ahead of time. Imposing a fairly small task on the programmer of declaring what he would use saved the tremendous effort of having the computer search the entire program for anything the programmer could use.

它还从具有使在每个源文件两次通过保存的编译器。当你处理,其中寻道时间分别以秒为单位和读取吞吐量在每秒字节数(不是千字节或兆字节)来测量磁带,这是pretty有意义的。

It also saved the compiler from having to make two passes on every source file: the first one to index all the symbols inside, and the second to parse the references and look them up. When you're dealing with magnetic tape where seek times were measured in seconds and read throughput was measured in bytes per second (not kilobytes or megabytes), that was pretty meaningful.

C ++中,而在近17年后,被定义为C的超集,因此,不得不使用相同的机制

C++, while created almost 17 years later, was defined as a superset of C, and therefore had to use the same mechanism.

通过将Java于1995年推出的左右时间,平均电脑有足够的内存,持有一个象征性的表,即使是一个复杂的工程,已不再是一个沉重的负担。和Java不被设计为用C向后兼容的,所以它没有必要采用一个遗留机制。 C#也同样束缚。

By the time Java rolled around in 1995, average computers had enough memory that holding a symbolic table, even for a complex project, was no longer a substantial burden. And Java wasn't designed to be backwards-compatible with C, so it had no need to adopt a legacy mechanism. C# was similarly unencumbered.

这样一来,他们的设计师选择了转向compartmentalizing象征性的声明背过程序员的负担,并把它再次在计算机上,因为它的成本比例编译的总工作量是最小的。

As a result, their designers chose to shift the burden of compartmentalizing symbolic declaration back off the programmer and put it on the computer again, since its cost in proportion to the total effort of compilation was minimal.

这篇关于为什么前向声明有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆