Files
wiki/代码规范/cppStyleGuide/StyleGuide.md
2024-07-15 15:24:45 +08:00

61 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# c++ 代码风格规范
## 源代码文件命名
##### 1. 前要
**通常情况下每个.cpp文件应该有一个配套的.h文件。当然也有例外的情况例如c++模板类的实现)。正确使用头文件会大大改善代码的可读性和执行文件的大小和性能.**
> 头文件应能独立编译,就是头文件的使用者和构建工具在导入文件时无需任何特殊的前提条件就能完成代码编译。具体来说,头文件要有头文件防护符,并导入它所需的所有其它头文件。
> 当编写c++模板时应保证模板的实现声明和实现都在同一个文件里。一般,写模板时源码文件以.hpp命名。
> 避免编写依赖某个构建工具的代码。例如项目的构建依赖于VSCode的一个插件使用其他的构建工具项目无法构建且生成
> 避免前向声明
##### 2. 文件命名
* **新建c++类[.h 或 .hpp]**
> 新建类文件时,尽量保证文件名与导出(外部调用)的类名保持一致(一个头文件中可能包含好几个类)。
> Help.h
```cpp
class Help{}
```
Help.hpp
```cpp
template<typename T>
class Help{}
```
##### 3. 防护符
* **要保证头文件带有防护重复引用预处理头**
```cpp
#pragma once
```
```cpp
#ifndef HELP_H
#define HELP_H
#endif
```
## 命名规范
##### 1. 命名空间[namespace]
##### 2. 类[class]
##### 3. 结构体[struct]
##### 4. 模板[template]
##### 5. 私有函数或变量[private]
##### 6. 共有函数或变量[public]
##### 7. 受保护函数或变量[protected]
##### 8. 预处理[#define]
##### 9. 内联函数[inline]
##### 10. 常量[const]
##### 11. 静态变量或函数[static]