C++ class header files organization

前端 未结 6 1941
孤街浪徒
孤街浪徒 2020-11-29 18:03

What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header fi

6条回答
  •  余生分开走
    2020-11-29 18:07

    I'd like to add one very good practice (both in C and C++) which is often forsaken :

    foo.c

    #include "foo.h" // always the first directive
    

    Any other needed headers should follow, then code. The point is that you almost always need that header for this compilation unit anyway and including it as a first directive warrants the header remains self-sufficient (if it is not, there will be errors). This is especially true for public headers.

    If at any point you need to put something before this header inclusion (except comments of course), then it is likely you're doing something wrong. Unless you really know what you are doing... which leads to another more crucial rule => comment your hacks !

提交回复
热议问题