个人整理的C/C++编程风格规范——摘自《Google开源项目风格指南》

文件名: 小写(必要时使用下划线) file_name.h

define保护:

#ifndef HELLO_H_
#define HELLO_H_
pass

#endif //HELLO_H_

变量名: 小写+下划线 variable_name

**全局变量: **g开头 g_variable_name

**静态变量: **s开头 s_variable_name

类成员变量: 小写+下划线+_ variable_name_

**常量名: **k+大驼峰 kConstName


函数名: 大驼峰 FunctionName

**类私有函数: **_+小写+下划线 _function_name

类名,结构体名,枚举名: 大驼峰 ClassName


空格:
二元运算符两端应有空格(+,-,*,/,=,>,<,>=,<=,||/or,&&/and等)
根据运算优先级,低优先级可以没有空格 (a+b) * (c+d)
函数形参要有空格 Function(int a, int b)
形参默认值等号两端不要空格 Function(int a=0)
左括号之后,右括号之前不要空格 (a, b, c, d)


空行: 顶级定义(函数、类)之间空两行,方法定义之间空一行

大型注释:

//==========
//xxxxxxxxxxxxxx
//xxxxxxxxxxxxxx
//==========

缩进: 每次缩进两格,严禁tab

类格式:

class ClassName{
 public: //缩进一格
 ClassName(); //构造函数
 ~ClassName(); //析构函数

 Function1();
 Function2();
  ...
//空一行

 protected:
 ...

 private:
 int x_, y_; //私有变量
}

函数格式: 参数写不下则缩进4格

FunctionName(
  int x,
  int y){
 return x+y;
}

输入在前,输出在后: Function(const int kInput,int *output)

发布了7 篇原创文章 · 获赞 12 · 访问量 502

猜你喜欢

转载自blog.csdn.net/weixin_45901207/article/details/105633195