摘自Google开源项目风格指南-C++风格指南

1.#define保护
使用#define防止头文件被多重包含

#ifndef <PROJECT>_<PATH>_<FILE>_H_
#define <PROJECT>_<PATH>_<FILE>_H_

#endif // <PROJECT>_<PATH>_<FILE>_H_

举个栗子~:

#ifndef STDIO_H_
#define STDIO_H_

#include <stdio.h>

#endif //stdio.h

2.函数参数的顺序
定义函数时, 参数顺序依次为: 输入参数, 然后是输出参数。

3.在 #include 中插入空行

以分割相关头文件, C 库, C++ 库, 其他库的 .h 和本项目内的 .h 是个好习惯。

举个栗子~:

#include “foo/public/fooserver.h” // 优先位置

#include <sys/types.h>
#include <unistd.h>
#include <hash_map>
#include <vector>

#include “base/basictypes.h”
#include “base/commandlineflags.h”
#include “foo/public/bar.h”

4.通用命名规则
(1)函数命名,变量命名,文件命名要有描述性;少用缩写。
尽可能给有描述性的命名,别心疼空间,毕竟让代码易于新读者理解很重要。
不要用只有项目开发者能理解的缩写,也不要通过砍掉几个字母来缩写单词。

举个栗子~:

int price_count_reader; // 无缩写
int num_errors; // “num” 本来就很常见
int num_dns_connections; // 人人都知道 “DNS” 是啥

(2)类型名称的每个单词首字母均大写, 不包含下划线: MyExcitingClass, MyExcitingEnum.

举个栗子~: 继续阅读摘自Google开源项目风格指南-C++风格指南