What should go into an .h file?

后端 未结 12 999
伪装坚强ぢ
伪装坚强ぢ 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: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

提交回复
热议问题