为什么 C++ 中函数声明是强制的而不是在C语言中?

发布于 2024-11-05 02:06:41 字数 79 浏览 0 评论 0原文

所以我之前的一次考试有这个问题,到目前为止我一直在读你不需要任何语言的声明?

哪个是对的?如果没有声明,C++会报错还是会运行?

So one of my previous exams had this question, and till now I've been reading that you don't need a declaration in any of the languages?

Which is right? Will C++ give an error if there's no declaration, or will it run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-11-12 02:06:41

在涉及 C 和 C++ 的讨论中,“函数声明”是一个相当模糊的术语。这些语言在这方面有很大不同。

在C++语言中,只有一种函数声明:带有所有参数类型和返回类型的声明。这样的声明是必要的,因为 C++ 语言支持函数重载。为了选择调用哪个函数,编译器需要了解该函数的所有信息,并且需要知道该函数的哪些重载版本可用。如果您“忘记”声明某个重载版本,重载决策将不会考虑它。这至少是 C++ 中需要函数声明的原因之一。

在 C 语言中,有两种函数声明:非原型声明和原型声明(或简称​​原型)。 C 中的原型与 C++ 声明非常相似 - 它包括所有参数类型。标准 C 中的可变参数函数(带有 ... 参数的函数)始终需要原型。对于非可变参数函数,即使在今天也不需要原型声明。但从 C99 开始,所有其他函数至少需要非原型声明。在较旧的 C89/90 版本中,不需要非可变参数函数的语言函数声明。

所以,这基本上应该回答你的问题了。在 C++ 中,函数声明是必需的,因为语言功能严重依赖它们。在现代 C 语言中,还需要函数声明,只是为了使代码更安全。在旧版本的 C 函数声明中,不需要函数声明,主要是因为该语言被定义为无需函数声明即可工作。

In a discussion that involves both C and C++ "function declaration" is a rather vague term. These languages are significantly different in this regard.

In C++ language there's only one kind of function declaration: declaration with all parameter types and return type. Such declarations are necessary because C++ language supports function overloading. In order to choose which function to call the compiler needs to know everything about the function and needs to know which overloaded versions of the function are available. If you "forget" to declare some overloaded version, it will not be considered by overload resolution. That is at least one of the reasons function declarations are necessary in C++.

In C language there are two kinds of function declarations: non-prototype declarations and prototype declarations (or simply prototypes). A prototype in C is pretty similar to C++ declaration - it includes all parameter types. Prototypes have always been required in standard C for variadic functions (functions with ... parameters). For non-variadic functions prototype declarations are not required even today. But starting from C99 at least non-prototype declarations are required for all other functions. In older C89/90 version of the language function declarations for non-variadic functions were not required.

So, that should basically answer your question. In C++ function declarations are required because language features rely on them critically. In modern C function declarations are also required just to make the code safer. In older versions of C function declarations were not required mostly simply because the language was defined to work without them.

又爬满兰若 2024-11-12 02:06:41

出于遗留/向后兼容性的原因,C 中的函数声明不是强制性的 - 如果它们被强制执行,那么某处的某些旧/遗留代码将停止编译。

我猜想它们在 C++ 中是强制性的,因为 C++ 不是 C 的严格超集,因此可以做出明智的选择,使它们成为强制性的。

但是,您应该始终声明它们 - 请参阅此问题必须在 C 中声明函数原型吗?

仅供参考,C99 函数声明现在是强制性的。

Function declarations in C are not mandatory for legacy / backwards compatability reasons - if they were made mandatory then some old / legacy code somewhere would stop compiling.

I'd guess that they are mandatory in C++ becasuse C++ isn't a strict superset of C and so can make the sensible choice of making them mandatory.

You should always declare them however - see this question Must declare function prototype in C?

FYI in C99 function declarations are now mandatory.

离鸿 2024-11-12 02:06:41

函数声明在 C 中是强制性的。然而,原型是可选的,除了可变参数函数和其参数类型将通过默认提升而改变的函数的情况。

Function declarations are mandatory in C. Prototypes, however, are optional, except in the cases of variadic functions and functions whose argument types would be altered by default promotions.

狼性发作 2024-11-12 02:06:41

我无法回答 C。

在 C++ 中,对于函数:

  • void foo(); 是一个声明。
  • void foo() { .. } 是一个定义和一个声明。

在调用 foo() 之前,您至少需要在作用域中进行一个声明。

对于数据变量来说也是如此。

I can't answer for C.

In C++, for functions:

  • void foo(); is a declaration.
  • void foo() { .. } is a definition and a declaration.

You need at least one declaration in scope before you call foo().

Much the same is true for data variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文