C++ class header files organization

前端 未结 6 1922
孤街浪徒
孤街浪徒 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:09

    Check out the C and C++ coding standards at the NASA Goddard Space Flight Center. The one rule that I specially noted in the C standard and have adopted in my own code is the one that enforces the 'standalone' nature of header files. In the implementation file xxx.cpp for the header xxx.h, ensure that xxx.h is the first header included. If the header is not self-contained at any time, then compilation will fail. It is a beautifully simple and effective rule.

    The only time it fails you is if you port between machines, and the xxx.h header includes, say, , but requires facilities that happen to be made available by a header on the original platform (so includes ), but the facilities are not made available by on the other platform (they are in def.h instead, but does not include ). This isn't a fault of the rule, and the problem is more easily diagnosed and fixed if you follow the rule.

提交回复
热议问题