Header files and include best practice

后端 未结 2 1243
慢半拍i
慢半拍i 2021-01-18 14:28

I have a quick question regarding header files, include statements, and good coding style. Suppose I have 2 classes with associated source and header files, and then a fina

2条回答
  •  孤城傲影
    2021-01-18 15:22

    Include all you need in every file, but do not include any file that you do not need. Normally, it is the job of the included file to make sure it is not included twice, using precompiler flags, etc...

    For example if is needed by Foo.cpp, but not by Foo.h, include it in Foo.cpp not in Foo.h. If required in both, include in both.

    Tangentially, as a best practice, never use using directives in a header file. If you need you can use using directives in implementation files (.cpp).

提交回复
热议问题