简体   繁体   English

C ++:包含头文件的最佳位置

[英]C++ : Best place to include header files

Let's say we have four files: ah, a.cpp, b1.h, and b2.h. 假设我们有四个文件:ah,a.cp​​p,b1.h和b2.h。 And, we need to include b1.h and b2.h in either ah or a.cpp. 并且,我们需要在ah或a.cpp中包括b1.h和b2.h。 Where should I include b1 and b2? 我应该在哪里包括b1和b2? Let's say only a.cpp needs b1 and b2. 假设只有a.cpp需要b1和b2。

If it's not needed for the header file then include it in the cpp. 如果头文件不需要它,则将其包含在cpp中。

This reduces compilation time and helps to understand the dependencies between the modules. 这样可以减少编译时间,并有助于了解模块之间的依赖性。

一般规则是,应避免在不使用其定义的标头中包含标头。

Only include what is needed in the header. 仅在标题中包含所需的内容。

Do the class definitions and function declarations of ah require b1.h or b2.h? ah的类定义和函数声明是否需要b1.h或b2.h? Then include what is needed. 然后包括所需的内容。

Otherwise only include in the .cpp. 否则,仅包括.cpp。

Just remember, each time you include a file your compilation takes that much longer. 请记住,每次包含文件时,编译所需的时间就更长。

Here are a couple hints of when things are needed: 以下是一些需要什么时的提示:

  • Return values or parameters do not need to be included. 不需要包含返回值或参数。 For example std::string blahFunc(std::string a); 例如std::string blahFunc(std::string a); does not need <string> included in the header file(still needs a forward declaration though) 不需要头文件中包含的<string> (尽管仍然需要一个前向声明)

  • Pointer types do not need to be included, they just need to be forward declared. 指针类型不需要被包括,它们只需要被前向声明。 For example randomType * f(); 例如randomType * f(); does not need to include randomType's header in it's header. 不需要在标头中包含randomType的标头。 All you have to do is forward declare with class randomType; 您要做的就是使用class randomType;向前声明class randomType;

  • References can also just be forward declared. 引用也可以只被向前声明。

If b1.h and/or b2.h have definitions like struct or typedef , etc. that are actually used in ah (in a function prototype as parameters or return type, for example), then you should include them in the top of the header. 如果b1.h和/或b2.h具有在ah中实际使用的定义(例如structtypedef等)(例如,在函数原型中作为参数或返回类型),则应将其包括在标头。

Otherwise, if b1.h/b2.h only provide definitions that are used internally to a.cpp (private members, etc), then include it at the top of a.cpp. 否则,如果b1.h / b2.h仅提供a.cpp内部使用的定义(私有成员等),则将其包括在a.cpp的顶部。

You should try to only include in a file what is actually needed for the compiler to understand that file. 您应该尝试仅在文件中包含编译器理解该文件实际需要的内容。 (Unlike windows, with the monstrosity that is <Windows.h> .) (与Windows不同,怪异的是<Windows.h> 。)

I think the include directives should always be in your .h** files. 我认为include指令应始终位于您的.h **文件中。 The only include you should put in the a.cpp file should be 您应该放入a.cpp文件中的唯一内容应该是

#include "a.hpp"

To understand why, suppose Bob uses your code inside his program, and your code is not disclosed (ie, you provide it only as a library). 为了理解原因,假设鲍勃在程序中使用了您的代码,并且您的代码未公开(即,您仅以库的形式提供)。 Then all he can see are your headers. 然后,他所看到的只是您的标题。 If you include everything you need in your headers, he will still be able to check what are the dependencies of your code and make sure he has everything needed by your code. 如果您在标头中包含所需的所有内容,那么他​​仍将能够检查代码的依赖项,并确保他具有代码所需的所有内容。 If you put the include directives in the .c** files, then unless your code is open source (ie he has access to the .c** files) he won't be able to see what are the packages he must make sure to have installed. 如果将include指令放在.c **文件中,那么除非您的代码是开源的(即他有权访问.c **文件),否则他将无法查看他必须确保安装的软件包已安装。

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

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