头文件与前向声明
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这当然是可能的。但对于实际大小的程序,将有大量函数需要在大量其他文件中声明。如果您在需要访问另一个函数的每个文件中放置前向声明,则会遇到许多问题:
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:
头文件包含前向声明 - 这就是它们的作用。他们解决的问题是当您有一个包含多个源代码文件的更复杂的项目时。
您可以拥有一个函数库,例如用于矩阵运算的
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 thematrix.c
functions into all the other source files. You would also have to keep all those copies up to date with any changes tomatrix.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.您可以使用前向声明,但它的扩展性不好,而且如果您使用其他人的代码或库,它会很笨重。
一般来说,头文件定义了代码的接口。
另外,想想如果函数需要某种用户定义的类型会发生什么。您也打算转发声明吗?该类型可能会定期更改其实现(保持其公共接口相同),这将导致必须定期更改所有前向声明。
头文件解决方案更易于维护(不易出错),并且可以更轻松地准确确定正在使用的代码。
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.
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.
它与
#include"add.h"
相同。如果您知道,预处理器会在您编写#include
指令的 .cpp 文件中扩展您在#include
中提到的文件。这意味着,如果您编写#include"add.h"
,您会得到相同的结果,就像您在执行“前向声明”一样。我假设
add.h
有这一行:什么是 C++ 中的前向声明?
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:What are forward declarations in C++?