openharmony-cpp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenHarmony C++ Coding Skills
OpenHarmony C++编码技能
Core Mandates (Common Pitfalls)
核心规则(常见陷阱)
These rules are strictly enforced in OpenHarmony and often differ from general C++ projects.
这些规则在OpenHarmony中被严格执行,且通常与通用C++项目有所不同。
1. Strict Naming & Formatting
1. 严格的命名与格式规范
- Extensions: Always use and
.cpp..h - Files: Filenames must match class names (Unix-like, e.g., ).
my_class.cpp - Variables: Global vars must start with (e.g.,
g_). Class members must end withg_config(e.g.,_).value_ - Braces: K&R Style is mandatory (opening brace on the same line).
- Details: See naming_formatting.md.
- 扩展名: 始终使用和
.cpp。.h - 文件: 文件名必须与类名匹配(类Unix风格,例如)。
my_class.cpp - 变量: 全局变量必须以开头(例如
g_)。类成员变量必须以g_config结尾(例如_)。value_ - 大括号: 强制使用K&R风格(左大括号与代码行在同一行)。
- 详情: 参见naming_formatting.md。
2. Header Management
2. 头文件管理
- Guards: Use guards. #pragma once is FORBIDDEN.
#ifndef - Includes: Prefer over forward declarations to prevent hidden dependencies.
#include - Details: See headers_scopes.md.
- 防护: 使用防护。禁止使用#pragma once。
#ifndef - 包含: 优先使用而非前向声明,以避免隐藏依赖。
#include - 详情: 参见headers_scopes.md。
3. Critical Security Requirements
3. 关键安全要求
- Input Validation: All external data must be validated before use (indices, sizes, loops).
- Memory Safety: Pointers must be set to immediately after deletion.
nullptr - Bitwise: Only on unsigned types.
- Details: See secure_coding.md.
- 输入验证: 所有外部数据在使用前必须经过验证(索引、大小、循环)。
- 内存安全: 指针在释放后必须立即设置为。
nullptr - 位操作: 仅允许对无符号类型进行位操作。
- 详情: 参见secure_coding.md。
Usage Guide
使用指南
When to use
使用场景
- New Code: Generating new classes or modules.
- Refactoring: Cleaning up legacy code.
- Review: Checking code against OpenHarmony standards.
- 新代码: 生成新类或模块。
- 重构: 清理遗留代码。
- 审核: 检查代码是否符合OpenHarmony标准。
Reference Map
参考文档映射
- naming_formatting.md: Naming conventions (g_, _), braces (K&R), line length.
- secure_coding.md: Input validation, integer safety, memory management, prohibited functions.
- class_function_design.md: Constructors, inheritance, modern C++ (nullptr, const), function size.
- headers_scopes.md: Header guards, namespaces, include rules.
- naming_formatting.md: 命名规范(g_, _)、大括号(K&R)、行长度。
- secure_coding.md: 输入验证、整数安全、内存管理、禁用函数。
- class_function_design.md: 构造函数、继承、现代C++(nullptr、const)、函数大小。
- headers_scopes.md: 头文件防护、命名空间、包含规则。