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

本文共 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

你可能感兴趣的文章
Word产品需求文档,已经过时了【转】
查看>>
dtoj#4299. 图(graph)
查看>>
关于网站的一些js和css常见问题的记录
查看>>
zabbix-3.4 触发器
查看>>
换用代理IP的Webbrowser方法
查看>>
【视频编解码·学习笔记】7. 熵编码算法:基础知识 & 哈夫曼编码
查看>>
spark集群安装部署
查看>>
MySql 查询表字段数
查看>>
mariadb 内存占用优化
查看>>
Centos7安装编译安装zabbix2.219及mariadb-5.5.46
查看>>
Visual Studio Remote Debugger(for 2005/2008) .net远程调试<转>
查看>>
怎么获得combobox的valueField值
查看>>
浅谈C/C++中的static和extern关键字
查看>>
Console-算法[if,while]-一输入两个正整数m和n,求其最大公约数和最小公倍数
查看>>
浅谈网络协议(四) IP的由来--DHCP与PXE
查看>>
jre与jdk的区别
查看>>
全景图的种类
查看>>
git 维护
查看>>
jfinal框架下使用c3P0连接池连接sql server 2008
查看>>
Jfinal Generator 不需要生成带某个前缀的表名数组的方法
查看>>