我在头文件中包括什么顺序? [英] What order do I include header files in?

查看:141
本文介绍了我在头文件中包括什么顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触编程,头文件的主题在我开始使用大量的头文件之后,有点令我失望。除此之外,我试图使用预编译头。我也使用SFML库,所以我有那些头,也必须包括。

I'm new to programming and the topic of header files is sort of bogging me down after I started using a lot of them. In addition to that I'm trying to use precompiled headers. I'm also using the SFML library so I have those headers that also have to be included.

现在我有stdafx.h,main.cpp,然后类A ,b,C和D包含在Ah,A.cpp,Bh,B.cpp,Ch,C.cpp,Dh和D.cpp中。

Right now I have stdafx.h, main.cpp, then classes A, B, C, and D contained within A.h, A.cpp, B.h, B.cpp, C.h, C.cpp, D.h, and D.cpp.

如果


  • 所有类都包含SFML类的实例,则我会在所有文件中包含标题

  • 类D包含类A和类C的实例

  • 类C包含类B的实例
    我的代码:(注意:所有头都有标题保护)

stdafx.h:

#include <SFML/Graphics.hpp>
#include <iostream>

Ah

#include "stdafx.h"
class A
{
    //sfml class
};

A.cpp

#include "stdafx.h"
#include "A.h"

Bh

#include "stdafx.h"
class B
{
    //sfml class
};

B.cpp

#include "stdafx.h"
#include "B.h"

Ch

#include "B.h"
class C: public B
{

};

C.cpp

#include "stdafx.h"
#include "C.h"

Dh

#include "A.h"
#include "C.h"
class D
{
    A a;
    C C; // if left uncommented I recieve a '1 unresolved externals' error
    //sfml class
}

D.cpp

#include "stdafx.h"
#include "D.h"

main.cpp

#include "stdafx.h"
#include "D.h"


解决方案

我的哲学是,在编写良好的代码中,头文件应该包括所有其他依赖的头文件。我的推理是,它不应该是可能的,包括头文件,并得到一个编译器错误这样做。因此,每个头文件应该( #ifdef #pragma once 包含警卫)包括所有其他依赖的标头。

My philosophy is that in well-written code, header files should include all other header files that they depend on. My reasoning is that it should not be possible to include a header file and get a compiler error for doing so. Therefore, each header file should (after the #ifdef or #pragma once include guard) include all other headers it depends on.

为了非正式测试你记得要在头文件中包含正确的头文件,* .cpp文件应该包含应该工作的最小头文件集。因此,如果有 A B C D ,而你的cpp文件使用类 D ,那么它应该只包括 Dh 。不会导致编译器错误,因为 Dh #include Ah Ch strong> Ch 包括 Bh​​ Ah Bh​​ 包括SFML标头(无论是什么)。 Ch Dh 可以包含SFML标题,如果它看起来合适,但它不是真的必要,如果你可以确保依赖( Bh​​

In order to informally test that you've remembered to include the right headers in your header files, *.cpp files should #include the minimum set of header files that should work. Therefore, if there are separate header files for A, B, C and D, and your cpp file uses class D, then it should only include D.h. No compiler errors should result, because D.h #includes A.h and C.h, C.h includes B.h, and A.h and B.h include the SFML header (whatever that is). C.h and D.h can include the SFML header if it seems appropriate, but it's not really necessary, if you can be sure that the dependencies (B.h and A.h) already included it.

然而,Visual C ++做的预编译头文件 需要StdAfx.h作为第一个头文件,这会导致许多开发人员将所有 StdAfx.h 中的整个项目中使用 > 其他头文件。我不推荐这个。或者,他们将把所有的外部依赖关系放在StdAfx.h中(例如windows.h,boost头文件)和#include其他地方的依赖关系,这样改变单个头文件不一定会导致整个项目重建。

The way Visual C++ does "precompiled headers" screws up this logic, however. It requires you to include "StdAfx.h" as the very first header file, which causes many developers to simply put all #includes for the entire project in StdAfx.h, and not to use #include in any of the other header files. I don't recommend this. Or, they will put all external dependencies in StdAfx.h (e.g. windows.h, boost headers) and #include the local dependencies elsewhere so that changing a single header file does not necessarily cause the entire project to rebuild.

我写我的代码的方式,我的大多数CPP文件包括StdAfx.h和相应的.H文件。所以A.cpp包括StdAfx.h和A.h,B.cpp包括StdAfx.h和B.h,等等。在cpp文件中放置的唯一其他 #include 是头文件不公开的内部依赖项。例如,如果 A 调用 printf(),则 A.cpp Ah )将 #include ,因为 Ah 不依赖于 stdio。 h

The way I write my code, most of my CPP files include StdAfx.h, and the corresponding .H file. So A.cpp includes StdAfx.h and A.h, B.cpp includes StdAfx.h and B.h, and so forth. The only other #includes placed in a cpp file are "internal" dependencies that are not exposed by the header file. For example, if class A calls printf(), then A.cpp (not A.h) would #include <stdio.h>, because A.h does not depend on stdio.h.

如果您遵循这些规则,则 #include 无关紧要(除非使用预编译头文件:那么预编译头文件首先出现在每个cpp文件中,但不需要包含在头文件中)。

If you follow these rules then the order that you #include headers does not matter (unless you use precompiled headers: then the precompiled header comes first in each cpp file, but does not need to be included from header files).

这篇关于我在头文件中包括什么顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆