头文件与前向声明

发布于 2024-10-13 01:32:53 字数 466 浏览 7 评论 0原文

http://www.learncpp.com/cpp-tutorial/19-header- files/

它提到以下内容作为“前向声明”的另一种解决方案:

头文件只需编写一次,并且可以根据需要包含在任意多个文件中。如果函数原型发生变化(例如通过添加新参数),这也可以最大限度地减少需要进行的更改数量,从而有助于维护。

但是,这也不能通过“前向声明”来实现吗?例如,由于我们在“add.cpp”中定义函数 int add(int x, int y),并通过键入以下内容在“main.cpp”中使用此函数:

int add(int x, int y);

? 谢谢。

http://www.learncpp.com/cpp-tutorial/19-header-files/

It mentions the following as another solution to "forward declaration":

A header file only has to be written once, and it can be included in as many files as needed. This also helps with maintenance by minimizing the number of changes that need to be made if a function prototype ever changes (eg. by adding a new parameter).

But, cannot this also be made with "forward declaration"? Since we are defining the function int add(int x, int y) for example in "add.cpp", and using this function in "main.cpp" by typing:

int add(int x, int y);

?
Thanks.

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

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

发布评论

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

评论(5

忆悲凉 2024-10-20 01:32:53

这当然是可能的。但对于实际大小的程序,将有大量函数需要在大量其他文件中声明。如果您在需要访问另一个函数的每个文件中放置前向声明,则会遇到许多问题:

  1. 您只是将相同的声明复制粘贴到许多不同的文件中。如果您更改了函数签名,则必须更改粘贴其前向声明的每个位置。
  2. 前向声明本身并不会自然地告诉您实际函数是在哪个文件中定义的。如果您使用合理的方法来组织头文件和源文件(例如,.cpp 文件中定义的每个函数都在 .cpp 文件中声明)。 h 文件同名),那么定义该函数的位置就隐含在它声明的位置。
  3. 您的代码对于其他程序员来说可读性较差,他们非常习惯使用头文件来完成所有事情(有充分的理由),即使您从头文件中需要的只是一个特定的函数,并且您可以轻松地自己转发声明它。

That is certainly possible. But for a realistically-sized program, there will be a large number of functions that a large number of other files will need to declare. If you put a forward declaration in every file that needs to access another function, you have a multitude of problems:

  1. You've just copy-pasted the same declaration into many different files. If you ever change the function signature, you have to change every place you've pasted its forward declaration.
  2. The forward declaration itself does not naturally tell you what file the actual function is defined in. If you use a sane method of organizing your header files and your source files (for instance, every function defined in a .cpp file is declared in a .h file with the same name), then the place that the function is defined is implied by the place that it is declared.
  3. Your code will be less readable to other programmers, who are very used to using header files for everything (for good reason), even if all you need from a header is one specific function and you could easily forward-declare it yourself.
鸠书 2024-10-20 01:32:53

头文件包含前向声明 - 这就是它们的作用。他们解决的问题是当您有一个包含多个源代码文件的更复杂的项目时。

您可以拥有一个函数库,例如用于矩阵运算的matrix.c。如果没有头文件,您必须将所有 matrix.c 函数的前向声明复制到所有其他源文件中。您还必须使所有这些副本与 matrix.c 的任何更改保持同步。

如果您更改了matrix.c 中的函数,但忘记在另一个文件中更改其声明,您将不会得到编译错误。您可能也不会收到链接器错误。一旦运行程序,您将遇到的只是崩溃或其他随机行为。

将声明放在单个文件(通常是 matrix.h)中,该文件将在其他地方使用,从而消除所有这些问题。

Header files contain forward declarations - that's what they do. The issue they resolve is when you have a more complex project with multiple source code files.

You could have a library of functions, e.g. matrix.c for matrix operations. Without header files you would have to copy the forward declarations for all the matrix.c functions into all the other source files. You would also have to keep all those copies up to date with any changes to matrix.c.

If you ever change the function in matrix.c, but forget to change its declaration in another file you will not get a compile error. You will probably not get a linker error either. All you will get is a crash or other random behaviour once you run your program.

Having the declarations in a single file, typically matrix.h, that will be used everywhere else removes all these issues.

徒留西风 2024-10-20 01:32:53

您可以使用前向声明,但它的扩展性不好,而且如果您使用其他人的代码或库,它会很笨重。

一般来说,头文件定义了代码的接口。

另外,想想如果函数需要某种用户定义的类型会发生什么。您也打算转发声明吗?该类型可能会定期更改其实现(保持其公共接口相同),这将导致必须定期更改所有前向声明。

头文件解决方案更易于维护(不易出错),并且可以更轻松地准确确定正在使用的代码。

You can use forward declaration but it doesn't scale well and it's unwieldly if you're using somebody else's code or library.

In general, the header file defines the interface to the code.

Also, think what happens if the function requires some user defined type. Are you going to forward declare that too? That type may regularly change its implementation (keeping it's public interface the same) which would result in having to regularly change all the forward declarations.

The header file solution is far more maintainable (less error prone) and make it far easier to determine exactly what code is being used.

凉墨 2024-10-20 01:32:53

IC 和 C++ 本质上将所有前向和/或外部声明放入标头中。这提供了一种将它们包含在各种源文件中的便捷方法,而无需手动包含它们。

就您而言,如果您在 add.cpp 中定义了 add,则只需在 main.cpp 中提供外部声明即可,一切都很酷。当您有大量需要添加声明的文件并且不想为每个文件都这样做时,头文件可以为您提供帮助。

I C and C++ one essentially put all the forward and or external declarations into the header. This then provides a convenient way of including them in the various source files without having to manually include them.

In your case, if you have add defined in add.cpp, you can just provide the external declaration in main.cpp and everything is cool. The header file is there to help you when you have a large number of files that need add declared and don't want to do so for each one.

枯寂 2024-10-20 01:32:53
int add(int x, int y); // forward declaration using function prototype

你能解释一下“前向声明”吗
更进一步?如果出现什么问题
我们在 main() 函数中使用它吗?

它与#include"add.h"相同。如果您知道,预处理器会在您编写 #include 指令的 .cpp 文件中扩展您在 #include 中提到的文件。这意味着,如果您编写 #include"add.h",您会得到相同的结果,就像您在执行“前向声明”一样。

我假设 add.h 有这一行:

int add(int x, int y); 

什么是 C++ 中的前向声明?

int add(int x, int y); // forward declaration using function prototype

Can you explain "forward declaration"
more further? What is the problem if
we use it in the main() function?

It's same as #include"add.h". If you know,preprocessor expands the file which you mention in #include, in the .cpp file where you write the #include directive. That means, if you write #include"add.h", you get the same thing, it is as if you doing "forward declaration".

I'm assuming that add.h has this line:

int add(int x, int y); 

What are forward declarations in C++?

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