博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于c++风格 code style
阅读量:6815 次
发布时间:2019-06-26

本文共 3914 字,大约阅读时间需要 13 分钟。

  1. Header files should be self-contained.
  2. All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be <PROJECT>_<PATH>_<FILE>_H_.
  3. You may forward declare ordinary classes in order to avoid unnecessary #includes. A "forward declaration" is a declaration of a class, function, or template without an associated definition. #include lines can often be replaced with forward declarations of whatever symbols are actually used by the client code.
  4. Use standard order for readability and to avoid hidden dependencies: Related header, C library, C++ library, other libraries' .h, your project's .h.
    • dir2/foo2.h.
    • C system files.
    • C++ system files.
    • Other libraries' .h files.
    • Your project's .h files.
  5. In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:
  6. Do not make nested classes public unless they are actually part of the interface, e.g., a class that holds a set of options for some method.
  7. Variables needed for if, while and for statements should normally be declared within those statements, so that such variables are confined to those scopes. E.g.:
  8. while (const char* p = strchr(str, '/')) str = p + 1;
  9. Constructors should never call virtual functions or attempt to raise non-fatal failures. If your object requires non-trivial initialization, consider using a factory function or Init() method.
  10. Use the C++ keyword explicit for constructors callable with one argument.
  11. Use delegating and inheriting constructors when they reduce code duplication.
  12. Inheriting constructors allow a derived class to have its base class's constructors available directly, just as with any of the base class's other member functions, instead of having to redeclare them. This is especially useful if the base has multiple constructors.
  13. Only very rarely is multiple implementation inheritance actually useful. We allow multiple inheritance only when at most one of the base classes has an implementation; all other base classes must be pure interface classes tagged with the Interface suffix. Multiple inheritance allows a sub-class to have more than one base class. We distinguish between base classes that are pure interfaces and those that have an implementation.
  14. All parameters passed by reference must be labeled const.
  15. Use streams only for logging.
  16. Use prefix form (++i) of the increment and decrement operators with iterators and other template objects.
  17. document that a variable is non-negative using assertions. Don't use an unsigned type.
  18. The names of variables and data members are all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member, a_class_data_member_. Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have.
  19. There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with g_ or some other marker to easily distinguish it from local variables.
  20. Regular functions have mixed case; accessors and mutators match the name of the variable: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
  21. Use a k followed by mixed case, e.g., kDaysInAWeek, for constants defined globally or within a class.const int kDaysInAWeek = 7;
  22. Enumerators should be named either like constants or like macros: either kEnumName or ENUM_NAME.
  23. Empty loop bodies should use {} or continue, but not a single semicolon.
  24. both of the && logical AND operators are at the end of the line.

摘自:http://google-styleguide.googlecode.com/svn/trunk/cppguide.html

关于命名空间:

命名空间和目录层相对应,不需要额外的缩进。

布尔表达式的逻辑操作永远放在行尾。

转载于:https://www.cnblogs.com/linyx/p/4153670.html

你可能感兴趣的文章
Android系统编译系统分析大全(二)
查看>>
git学习
查看>>
一篇笔记整理JVM工作原理
查看>>
ETL大数据测试介绍
查看>>
利用tar 通过网络拷贝数据
查看>>
CSS中的绝对定位与相对定位
查看>>
ubuntu 修改系统时间
查看>>
Struts2页面到action的三种传值方式
查看>>
【算法导论】插入排序
查看>>
html5定位getLocation()
查看>>
centos7生产环境下openssh升级
查看>>
[原创]Win2003+IE8+VS2005不能进入调试状态
查看>>
[HNOI2004]宠物收养场
查看>>
BZOJ3781:小B的询问(莫队)
查看>>
Ruby实现wordCounter
查看>>
SOAP Services for Python
查看>>
js判断处理undefined类型的数据
查看>>
经验&&bug
查看>>
【leetcode】96. Unique Binary Search Trees
查看>>
【leetcode】552. Student Attendance Record II
查看>>