为什么一个函数需要它的定义,或者使用之前需要申报? [英] Why does a function need to be declared before it's defined or used?
问题描述
在C及其可选。在C ++中有一个 MUST的声明函数的使用/定义之前。为什么会这样呢?请告诉我需要什么?我们不这样做,在C#或Java。
In C its optional. In C++ one "MUST" declare a function before its used/defined. Why is it so? Whats the need? We don't do that in C# or Java.
有趣的是虽然我们的确定的功能。该定义本身具有声明即使这样,我们需要声明。上帝知道为什么吗?
Funny thing is while we are defining a function. The definition itself has a declaration even then, we need to declare. God knows why?
推荐答案
滑稽你提到的是,就在本周埃里克利珀写您问题相关的博客文章:
Funny that you mention that, just this week Eric Lippert wrote a blog post related to your question :
HTTP://blogs.msdn。 COM / ericlippert /存档/ 2010/02/04 /如何一对多-passes.aspx
基本上,这是关系到编译器是如何工作的。在C#和Java编译器做出几个通行证。如果他们遇到调用一个尚未知的方法,这不是一个错误,因为定义可能是后来才发现,呼叫将在下一次传球来解决。请注意,我的解释过于简单,我建议你阅读埃里克利珀的帖子更完整的答案......
Basically, this is related to how the compiler works. The C# and Java compilers make several passes. If they encounter a call to a method that is not yet known, that's not an error, because the definition might be found later and the call will be resolved at the next pass. Note that my explanation is overly simplistic, I suggest you read Eric Lippert's post for a more complete answer...
这篇关于为什么一个函数需要它的定义,或者使用之前需要申报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!