简体   繁体   English

函数原型-C ++

[英]Prototyping a function - C++

Lately, some of my CPP tutorials have used function prototypes . 最近,我的一些CPP教程都使用了函数原型 I understand you must initialize the function, but what is the overall use of it? 我了解您必须初始化该函数,但是它的整体用途是什么? Couldn't you use just as well write the entire function before main() instead of defining a prototype? 您能不能像在main()之前那样编写整个函数,而不是定义原型?

int returnValue(void);

int main()
{
  std::cout << returnValue() << std::endl;
  return 0;
}

int returnValue(void)
{
  return 10;
}

Couldn't you use just as well write the entire function before main() instead of defining a prototype? 您能不能像在main()之前那样编写整个函数,而不是定义原型?

I can think of the following cases where you cannot. 我可以想到以下无法解决的情况。

Functions used in multiple files 多个文件中使用的功能

If a function is used in multiple source (.cpp) files, you can define the function only in one source file. 如果在多个源(.cpp)文件中使用了功能,则只能在一个源文件中定义该功能。 You have to declare it in the remaining source files. 您必须在其余的源文件中声明它。 For convenience and to avoid errors, such declarations are put in header files. 为了方便和避免错误,将此类声明放在头文件中。 The header files are then #include d by the source files. 然后,头文件由源文件#include d。

Mutually recursive functions 相互递归函数

If foo calls bar and bar calls foo , you cannot implement them without providing a declaration of at least one of the functions. 如果foo调用bar并且bar调用foo ,则不能在不提供至少一个函数声明的情况下实现它们。

As a matter of good practice, it's better to declare both functions first. 作为一种好的做法,最好先声明两个函数。 Then you can implement them in any order. 然后,您可以按任何顺序实施它们。

One important usage case is when you separate your implementation from the declarations. 一种重要的使用情况是将实现与声明分开。 In other words, you declare your functions/classes etc in a header file, and define (ie implement) them in cpp files. 换句话说,您在头文件中声明函数/类等,并在cpp文件中定义(即实现)它们。 In this way, you can distribute your program with the implementation fully compiled in a shared or static library. 这样,您可以在共享或静态库中完全编译了实现的情况下分发程序。 In order to use a pre-compiled function you need to introduce it to your program via a declaration. 为了使用预编译的函数,您需要通过声明将其引入程序。 Example: 例:

ah

void f();

a.cpp cpp文件

void f(){/* implementation here */}

main.cpp main.cpp

#include "a.h"

int main()
{
    f();
}

Including "ah" in main() is declaring the function. main()包含"ah"是对函数的声明。 Once you compile the a.cpp once, you don't need it's source any more, the program will run provided you have at least access to the object file, but in order for the linker to find the function f() you need to declare it. 一旦编译了a.cpp ,就不再需要它的源代码,只要您至少具有对目标文件的访问权限,该程序就会运行,但是为了使链接程序找到函数f()您需要声明它。

If one doesn't specify the function prototype, the behavior is specific to C standard (either C90 or C99) that the compilers implement. 如果未指定函数原型,则该行为特定于编译器实现的C标准(C90或C99)。 Up to C90 standard, C compilers assumed the return type of the omitted function prototype as int. 直到C90标准,C编译器都将省略的函数原型的返回类型假定为int。 And this assumption at compiler side may lead to unspecified program behavior. 并且在编译器端的这种假设可能导致未指定的程序行为。

Later C99 standard specified that compilers can no longer assume return type as int. 后来的C99标准指定编译器不能再将返回类型假定为int。 Therefore, C99 became more restrict in type checking of function prototype. 因此,C99在功能原型的类型检查中变得更加严格。 But to make C99 standard backward compatible, in practice, compilers throw the warning saying that the return type is assumed as int. 但是为了使C99标准向后兼容,实际上,编译器会发出警告,指出返回类型假定为int。 But they go ahead with compilation. 但是他们继续进行编译。 Thus, it becomes the responsibility of programmers to make sure that the assumed function prototype and the actual function type matches. 因此,程序员有责任确保假定的函数原型与实际的函数类型匹配。

To avoid all this implementation specifics of C standards, it is best to have function prototype. 为了避免所有这些C标准的实现细节,最好具有功能原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM