简体   繁体   English

.h(头文件)和.cpp文件有什么区别?

[英]What is the difference between a .h(header file) and a .cpp file?

I am creating a windows:forms application. 我正在创建一个窗口:表单应用程序。 I have read some of the answers give to try to understand the concept of .h(header file) & .cpp(implementation files). 我已经阅读了一些答案,试图理解.h(头文件)和.cpp(实现文件)的概念。 As I created the GUI for my app. 当我为我的应用程序创建GUI时。 I noticed the code being placed in the .h file. 我注意到代码放在.h文件中。 But when I double clicked a button control to add code to the procedure, the code was created in the .h file instead of the .cpp file. 但是,当我双击按钮控件以向过程添加代码时,代码是在.h文件而不是.cpp文件中创建的。 Do I cut and place this code into the .cpp file or is this where linking come in? 我是否将此代码剪切并放入.cpp文件中,或者这是链接进来的地方? The procedure deffintion stays in the .h file and will be linked to the procedure code in the .cpp file. 过程deffintion保留在.h文件中,并将链接到.cpp文件中的过程代码。

As others have stated the header file is meant for class, function, type etc. declarations while the .cpp file should contain the implementations of the functions. 正如其他人所说,头文件用于类,函数,类型等声明,而.cpp文件应该包含函数的实现。 However, a compiler does not care where the implementation code is written so you can put all your code in the header itself. 但是,编译器并不关心编写实现代码的位置,因此您可以将所有代码放在标头本身中。 But doing so will increase the compilation time because all that code will be compiled in every file that you include that header file in. 但这样做会增加编译时间,因为所有代码都将编译在包含该头文件的每个文件中。

Keeping the implementation separate is very useful if you want to distribute your code but need to keep proprietary code hidden. 如果要分发代码但需要隐藏专有代码,那么保持实现分离非常有用。 In this case you can create a library from the implementation code and distribute the header along with the library. 在这种情况下,您可以从实现代码创建库,并将标头与库一起分发。

The exception to this is when writing template code; 例外情况是编写模板代码时; in that case the implementation must go into the header because the compiler needs to 'see' it at the callsite. 在这种情况下,实现必须进入头部,因为编译器需要在调用点“看到”它。

There are two considerations here. 这里有两个考虑因素。 First off, header files are not nearly as important in managed code as they are in native C or C++. 首先,头文件在托管代码中并不像在本机C或C ++中那么重要。 Managed code compilers read declarations from the assembly metadata, you don't (and shouldn't) write C++/CLI type declarations that need to be visible in other modules in header files. 托管代码编译器从程序集元数据中读取声明,您不会(也不应该)编写需要在头文件中的其他模块中可见的C ++ / CLI类型声明。 Getting them from the metadata makes your types available to any .NET language. 从元数据中获取它们使您的类型可用于任何 .NET语言。

But the real reason is because of the way the form designer works in the C++ IDE. 但真正的原因是因为表单设计器在C ++ IDE中的工作方式。 It is a code generator, driven by the controls you drop on a form or user control and the properties you set in the Properties window. 它是一个代码生成器,由您在表单或用户控件上放置的控件以及在“属性”窗口中设置的属性驱动。 There is a very awkward problem if that code generator needs to generate code in two separate files. 如果代码生成器需要在两个单独的文件中生成代码,则存在一个非常棘手的问题。 Not so much the generating bit, but removing code when you alter the design of the form. 不是生成位,而是在更改表单设计时删除代码。 Getting the files out of sync produces difficult to diagnose compile errors from auto-generated code. 使文件不同步会导致难以从自动生成的代码中诊断编译错误。 It is a lot more likely to happen than you might think btw, the code generator needs to work with source code files that are in the process of being edited. 它比你想象的更有可能发生,代码生成器需要处理正在编辑的源代码文件。 Very difficult, the code might not parse at all. 非常困难,代码可能根本无法解析。

The C++ IDE uses the original approach taken by the version 1.1 C# and VB.NET designers, everything goes in one file. C ++ IDE使用1.1版C#和VB.NET设计者采用的原始方法,一切都在一个文件中。 Which is unpleasant of course, declarations and code shouldn't be mixed. 当然,这是不愉快的,声明和代码不应该混在一起。 That was solved in those designers for version 2.0 by adding support to the C# and VB.NET languages for partial classes. 通过为部分类添加对C#和VB.NET语言的支持,在2.0版本的设计器中解决了这个问题。 That however didn't happen for C++/CLI. 然而,C ++ / CLI没有发生这种情况。 Not sure why, the C++/CLI team always looked resource constrained compared to those other teams. 不确定原因,与其他团队相比,C ++ / CLI团队总是看起来资源有限。 It also doesn't have any kind of refactoring support, which is very important to rename methods when the class name changes. 它也没有任何重构支持,这在类名更改时重命名方法非常重要。 Keeping the methods inline avoids the problem. 保持方法内联可以避免这个问题。

Anyhoo, you can get your code in the .cpp file but you have to do it yourself. Anyhoo,您可以在.cpp文件中获取代码,但您必须自己完成。 Cut+paste gets it done, but you'll also have to edit the class name back into the method declarations. 剪切+粘贴完成它,但您还必须将类名称编辑回方法声明。 This is high maintenance, consider if it is worth the effort. 这是高维护,考虑是否值得努力。 Also consider whether you really want to use C++/CLI for the UI, it is an uncommon choice. 还要考虑您是否真的想要为用户界面使用C ++ / CLI,这是一个不寻常的选择。

Function prototypes (you called them procedure definitions) should go in the header file (along with macros). 函数原型(您称之为过程定义)应该放在头文件中(以及宏)。 Any code should go in the .cpp file. 任何代码都应该放在.cpp文件中。

If you're sure that what you're thinking of moving is code, it should be moved there. 如果你确定你想要移动的是代码,它应该被移动到那里。 It would help to paste some code. 粘贴一些代码会很有帮助。

The header (*.h) files are suppose to contain definitions of your classes, methods and types while the cpp files suppose to have the implementation details. 标题(* .h)文件假定包含类,方法和类型的定义,而cpp文件假定具有实现细节。 It is common to have inline and short methods in the header files as well. 在头文件中也常常使用内联和短方法。

This practice is not enforced by the compiler so you can write implementation (ie code) inside the header file. 编译器不强制执行此操作,因此您可以在头文件中编写实现(即代码)。

Personally I'm moving that automatically generated code to *.cpp files. 就个人而言,我正在将自动生成的代码移动到* .cpp文件中。 In *.h files I'm leaving functions which are not going to be changed very often. 在* .h文件中,我要离开的功能不会经常更改。 I'm doing that to be sure that compilation times will not grow to much after every change I made. 我这样做是为了确保在我做出的每一次改变之后,编译时间不会增长太多。

暂无
暂无

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

相关问题 .cpp 文件和 .h 文件有什么区别? - What is the difference between a .cpp file and a .h file? 在标头而不是源文件(cpp / h)中声明变量之间有什么区别 - What is the difference between declaring a variable in a header instead of the source file (cpp/h) 在.h 文件中的 class 中声明 static 变量和在 .cpp 文件中声明“全局”变量有什么区别 - What is the difference between declaring a static variable in a class in the .h file and a “global” variable in the .cpp file 在.h文件或.cpp文件中实现类之间的区别 - Difference between implementing a class inside a .h file or in a .cpp file 在头文件中定义的静态对象和在 cpp 文件中定义的静态对象有什么区别? - What is the difference between static objects defined in a header file and static objects defined in a cpp file? 全局const初始化以及.h或.cpp文件中的构造函数之间的差异 - Global const initializing and difference between constructor in .h or .cpp file 将头文件(.h)放在另一个文件(.h 或 .cpp)的开头和结尾的区别 - difference between putting head file(.h) at the beginning and end of another file(.h or .cpp) 包含.cpp文件和.h文件(在cpp中具有相同内容)的区别? - Difference in including the .cpp file and .h file (with the same content in cpp)? header 和库文件有什么区别? - What is the difference between header and library file? 头文件和命名空间有什么区别? - What is the difference between header file and namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM