What should go into an .h file?

后端 未结 12 828
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 15:26

When dividing your code up into multiple files just what exactly should go into an .h file and what should go into a .cpp file?

相关标签:
12条回答
  • 2020-11-22 15:34

    Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, "definitions".

    Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, "implementations".

    The simplest question to ask yourself to determine what belongs where is "if I change this, will I have to change code in other files to make things compile again?" If the answer is "yes" it probably belongs in the header file; if the answer is "no" it probably belongs in the code file.

    0 讨论(0)
  • 2020-11-22 15:38

    Header (.h)

    • Macros and includes needed for the interfaces (as few as possible)
    • The declaration of the functions and classes
    • Documentation of the interface
    • Declaration of inline functions/methods, if any
    • extern to global variables (if any)

    Body (.cpp)

    • Rest of macros and includes
    • Include the header of the module
    • Definition of functions and methods
    • Global variables (if any)

    As a rule of thumb, you put the "shared" part of the module on the .h (the part that other modules needs to be able to see) and the "not shared" part on the .cpp

    PD: Yes, I've included global variables. I've used them some times and it's important not to define them on the headers, or you'll get a lot of modules, each defining its own variable.

    EDIT: Modified after the comment of David

    0 讨论(0)
  • 2020-11-22 15:42

    I'd expect to see:

    • declarations
    • comments
    • definitions marked inline
    • templates

    the really answer though is what not to put in:

    • definitons (can lead to things being multiply defined)
    • using declarations/directives (forces them on anyone including your header, can cause nameclashes)
    0 讨论(0)
  • 2020-11-22 15:42

    The header Defines something but doesn't tell anything about the implementation. ( Excluding Templates in this "metafore".

    With that said, you need to divide "definitions" into sub-groups, there are, in this case, two types of definitions.

    • You define the "layout" of your strucutre, telling only as much as is needed by the surrounding usage groups.
    • The definitions of a variable, function and a class.

    Now, I am of course talking about the first subgroup.

    The header is there to define the layout of your structure in order to help the rest of the software use the implementation. You might want to see it as an "abstraction" of your implementation, which is vaughly said but, I think it suits quite well in this case.

    As previous posters have said and shown you declare private and public usage areas and their headers, this also includes private and public variables. Now, I don't want to go into design of the code here but, you might want to consider what you put in your headers, since that is the Layer between the end user and the implementation.

    0 讨论(0)
  • 2020-11-22 15:44

    Mainly header file contain class skeleton or declaration (does not change frequently)

    and cpp file contains class implementation (changes frequently).

    0 讨论(0)
  • 2020-11-22 15:48

    What compiles into nothing (zero binary footprint) goes into header file.

    Variables do not compile into nothing, but type declarations do (coz they only describe how variables behave).

    functions do not, but inline functions do (or macros), because they produce code only where called.

    templates are not code, they are only a recipe for creating code. so they also go in h files.

    0 讨论(0)
提交回复
热议问题