What order do I include header files in?

前端 未结 5 1413
挽巷
挽巷 2021-02-15 11:26

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.

5条回答
  •  一整个雨季
    2021-02-15 12:16

    A.h should include SFML

    A.cpp should include A.h

    D.h should include SFML, A.h and C.h

    D.cpp should include D.h

    main.cpp should include whichever of A, B, C, D and SFML that it uses directly.

    Generally in a .cpp file I don't include any headers that you know must be included by its corresponding .h because they contain the definitions of data members of classes defined in that .h. Hence, in my code D.cpp would not include A.h. That's just me, though, you might prefer to include it anyway just to remind you that the .cpp file (presumably) uses it.

    This leaves stdafx - where you need this depends what uses the things in it. Probably it's needed everywhere, and MSVC doesn't process (or processes but discards?) anything before #include "stdafx.h" in a source file, so it should be the first thing in each .cpp file and appear nowhere else.

    All header files should have multiple-include guards.

    You could add SFML (or anything else you feel like) to stdafx.h, in which case you might as well remove those includes from everywhere else.

    Once you've done this, it no longer matters what order you include the headers in each file. So you can do what you like, but I recommend Google's C++ style guide on the subject (click on the arrow thing), adjusted to account for stdafx.h.

提交回复
热议问题