为什么函数声明必须在C ++中,而不是用C? [英] Why are function declaration mandatory in C++ and not in C?

查看:231
本文介绍了为什么函数声明必须在C ++中,而不是用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的previous考试的人有这个问题,直到现在我一直在读,你不需要任何语言的声明?

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?

哪个是正确的?按c ++给一个错误,如果没有申报,或者它运行?

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

推荐答案

在这既包括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.

在C ++语言只有一种类型的函数声明:声明与所有的参数类型和返回类型。因为C ++语言支持函数重载这样的声明是必要的。为了选择哪个函数来调用编译器需要了解的一切功能,并需要知道重载函数的版本。 (如果你忘了声明一些重载版本,它不会被超载的分辨率考虑。)这就是为什么(的原因至少一个)函数声明都需要在C ++中。

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 overloaded resolution.) That is why (at least one of the reasons) function declarations are required in C++.

在C语言中有两种函数声明:非原型声明和原型。原型C是类似于C ++声明pretty - 它包括了所有参数类型。原型一直要求在标准C可变参数的功能。对于非可变参数的函数原型不需要这一天,但至少需要在语言的版本C99其他功能的非原型声明。在旧的C89 / 90版本的语言函数声明不是必需的。

In C language there are two kinds of function declarations: a non-prototype declaration and a prototype. 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. For non-variadic functions prototypes are not required to this day, but at least a non-prototype declaration is required for all other functions in C99 version of the language. In older C89/90 version of the language function declarations are not required.

所以,应该基本上回答你的问题。在C ++函数声明必需的,因为语言功能极其依靠他们。在现代C也需要函数的声明只是为了让code更安全。在老版本的C函数声明不要求大多只是因为语言定义,没有他们的工作。

So, that should basically answer your question. In C++ function declarations are required because language features critically rely on them. 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.

这篇关于为什么函数声明必须在C ++中,而不是用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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