当前位置: 首页 > news >正文

C++ 编码规范

编程风格整理

以下只针对自定义,三方库保持原样

类名:大驼峰 PascalCase(首字母大写,无下划线)

文件名:全小写 + 下划线(与类名完全对应)

文件夹:全小写

缩写:

Calibration → Calib Parameter → Param Initialize → Init Configuration → Config Manager → Mgr(可选) Utility → Util (工具) Calculate → Calc Process → Proc Communication → Comm (通信)

1.License声明 每个文件都需要

/***************************************************************************** * * * OpenNI 1.x Alpha * * Copyright (C) 2012 PrimeSense Ltd. * * * * This file is part of OpenNI. * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * * *****************************************************************************/

2.头文件

2.1 条件编译

#ifndef __XN_OPEN_NI_H__ #define __XN_OPEN_NI_H__ #endif // __XN_OPEN_NI_H__

上面是传统写法

国内项目、Qt 开发、Windows/Linux 开发:全部统一用: #pragma once(现代写法,推荐)

2.2 #Include

//--------------------------------------------------------------------------- // Includes //--------------------------------------------------------------------------- #include "XnTypes.h"

2.3 #define

//--------------------------------------------------------------------------- // Defines //--------------------------------------------------------------------------- #define XN_MASK_OPEN_NI "OpenNI"

2.4 struct

//--------------------------------------------------------------------------- // Types //--------------------------------------------------------------------------- #pragma pack(push, 1) /** * The base of the building block of the data types - XnBaseNode */ typedef struct XnBaseNode { /** the next XnNode */ XnBaseNode* m_pNext; /** the previous XnNode */ XnBaseNode* m_pPrevious; /** the value of the XnNode */ XnValue m_Data; } XnBaseNode; #pragma pack(pop)

2.5 enum

//--------------------------------------------------------------------------- // Types //--------------------------------------------------------------------------- typedef enum { /** Control is idle **/ DEVICE_CONTROL_CLEAR, /** Control request was received **/ DEVICE_CONTROL_REQUEST_RECEIVED, /** Control request was read by device, no reply yet **/ DEVICE_CONTROL_REQUEST_READ, /** Control reply received, waiting for host in-request **/ DEVICE_CONTROL_REPLY_READY, } DeviceControlState;
//--------------------------------------------------------------------------- // Structures & Enums //--------------------------------------------------------------------------- // Network socket type /** The network socket type. */ typedef enum { /** UDP socket. */ XN_OS_UDP_SOCKET = 0, /** TCP socket. */ XN_OS_TCP_SOCKET } XnOSSocketType;

2.6 全局变量

//--------------------------------------------------------------------------- // Global Variables //--------------------------------------------------------------------------- static XnMemBlockDataLinkedList g_allocatedMemory = {NULL, NULL}; static XN_CRITICAL_SECTION_HANDLE g_hCS; static XnDumpFile* g_dump = NULL;

2.7 重定义内部使用类型

//--------------------------------------------------------------------------- // Basic Types //--------------------------------------------------------------------------- /** Boolean TRUE/FALSE type. */ typedef BOOL XnBool; /** Signed character for strings. */ typedef char XnChar; /** Unsigned character for strings. */ typedef unsigned char XnUChar; /** Signed wide character for strings. */ typedef wchar_t XnWChar; /** 8-bit signed integer. */ typedef signed char XnInt8; /** 8-bit unsigned integer. */ typedef unsigned char XnUInt8; /** 16-bit signed integer. */ typedef short XnInt16; /** 16-bit unsigned integer. */ typedef unsigned short XnUInt16; /** 32-bit signed integer. */ typedef int XnInt32; /** 32-bit unsigned integer. */ typedef unsigned int XnUInt32; /** 64-bit signed integer. */ typedef __int64 XnInt64; /** 64-bit unsigned integer. */ typedef unsigned __int64 XnUInt64; /** natural signed integer. */ typedef int XnInt; /** natural unsigned integer. */ typedef unsigned int XnUInt; /** Float (32bit) */ typedef float XnFloat; /** Double (64bit) */ typedef double XnDouble;

2.8 API

//--------------------------------------------------------------------------- // API //--------------------------------------------------------------------------- /** * Converts a Xiron Status enumerator into a meaningful error string. * * @param Status [in] The input Xiron Status to be converted to a string. * * @return A string representation of the Xiron status. */ XN_C_API const XnChar* XN_C_DECL xnGetStatusString(const XnStatus Status); /** * Allocates a node info object, and sets its details. * * @param pDescription [in] The description for the new node info object. * @param strCreationInfo [in] The creation info for the new node info object. * @param pNeededNodes [in] A list of node info's that are needed by the new node info. * @param ppNodeInfo [out] A pointer to pointer to the new node info object. */ XN_C_API XnStatus XN_C_DECL xnNodeInfoAllocate(const XnProductionNodeDescription* pDescription, const XnChar* strCreationInfo, XnNodeInfoList* pNeededNodes, XnNodeInfo** ppNodeInfo);

2.9变量命名规则

类型 m_小写类型名字 type m_tName; int m_nAge; bool m_bActiveState; char* m_pstrName; int* m_pnAge; type m_tName_; type t_tName;

3.注释

3.1 文件内部模块注释

// Shared libraries XN_C_API XnStatus XN_C_DECL xnOSLoadLibrary(const XnChar* cpFileName, XN_LIB_HANDLE* pLibHandle); XN_C_API XnStatus XN_C_DECL xnOSFreeLibrary(const XN_LIB_HANDLE LibHandle); // Time XN_C_API XnStatus XN_C_DECL xnOSGetEpochTime(XnUInt32* nEpochTime); XN_C_API XnStatus XN_C_DECL xnOSGetTimeStamp(XnUInt64* nTimeStamp);
XN_C_API XnStatus xnInit(XnContext** ppContext) { XnStatus nRetVal = XN_STATUS_OK; XN_VALIDATE_OUTPUT_PTR(ppContext); // make sure xnOS is initialized nRetVal = xnOSInit(); if (nRetVal != XN_STATUS_OK && nRetVal != XN_STATUS_OS_ALREADY_INIT) { return (nRetVal); } // and also log system xnLogInitSystem(); *ppContext = NULL; // allocate context XnContext* pContext; XN_VALIDATE_NEW(pContext, XnContext); }

3.2 单个函数或者变量注释

/**..........*/ /** * Allocates a node info object, and sets its details. * * @param pDescription [in] The description for the new node info object. * @param strCreationInfo [in] The creation info for the new node info object. * @param pNeededNodes [in] A list of node info's that are needed by the new node info. * @param ppNodeInfo [out] A pointer to pointer to the new node info object. */ XN_C_API XnStatus XN_C_DECL xnNodeInfoAllocate(const XnProductionNodeDescription* pDescription, const XnChar* strCreationInfo, XnNodeInfoList* pNeededNodes, XnNodeInfo** ppNodeInfo);

4.类 头文件命名

  1. 谷歌 C++ 规范(最主流)
    这是目前工业界使用最广泛的规范之一,规则清晰且易落地:
    类名:采用PascalCase(大驼峰),每个单词首字母大写,无下划线。
    示例:UserInfo、HttpRequest、DataManager
    头文件 / 源文件:全小写,单词之间用下划线_分隔,后缀分别为.h/.cpp(或.cc/.cxx)。
    示例:类UserInfo对应头文件user_info.h、源文件user_info.cpp

  2. Qt / 微软规范
    偏向 Windows/Qt 生态的命名风格:
    类名:同谷歌规范,PascalCase(大驼峰)。
    示例:QString、MainWindow、FileDialog
    头文件 / 源文件:与类名完全一致(大小写相同),后缀.h/.cpp。
    示例:类MainWindow对应MainWindow.h、MainWindow.cpp

  3. Linux 内核 / 开源库规范(全小写 + 下划线)
    偏向 Unix/Linux 生态的极简风格:
    类名:全小写,单词间用下划线分隔(注:Linux 内核本身少用类,但开源库如 Boost 部分模块采用此风格)。
    示例:user_info、http_request
    头文件 / 源文件:与类名一致,全小写 + 下划线,后缀.h/.c(或.cpp)。
    示例:类user_info对应user_info.h、user_info.cpp

5.其他

SDK的C接口中不支持引用,采用指针

http://www.jsqmd.com/news/952027/

相关文章:

  • 2026年大客户营销咨询选购指南,品牌排名 - mypinpai
  • 别再死记硬背!一张图+一个故事帮你理清正交、酉、正规矩阵的关系与区别
  • Zotero PDF预览插件:让文献浏览告别窗口切换的困扰
  • Transformer QKV 计算瓶颈?一次关于长上下文显存爆炸的硬核排查与优化
  • AI简历不是“加个ChatGPT”,而是重构求职链路——12个企业级落地案例拆解
  • 2026年深圳全屋定制一站式服务避坑 别被假工厂全流程忽悠了 - 产品测评官
  • 智能担保系统架构设计全图解(含LLM+规则引擎双模决策链路)
  • 别再死记硬背了!用Multisim/PSpice仿真带你直观理解PFC的三种工作模式(CCM/DCM/CrM)
  • PPTist:5分钟打造专业演示文稿的终极免费在线PPT制作工具
  • Mac窗口置顶神器Topit:如何让重要窗口永远在最前方
  • 紧急预警:标注数据漂移正 silently 毁掉你的模型效果!——用AI工具构建动态标注质量监控仪表盘(Python+Prometheus实战)
  • CentOS 7生产环境PHP 8.1安装避坑实录:Remi源、扩展冲突与SELinux策略
  • ov5647摄像头模块、MIPI的MCLK主时钟
  • 2026年酒泉驾考驾校价格比较:新亿阳驾校性价比高吗? - mypinpai
  • 从开关电源到第三代半导体:聊聊PFC技术这几十年的‘进化史’
  • Python 爬虫进阶技巧:自定义请求头编码适配多国语言网页爬取
  • 3步掌握iOS虚拟定位:iFakeLocation完全指南
  • 教育AI整合进入“深水区”:2024Q2行业报告显示,仅17%机构实现L1-L4能力跃迁——你的团队处在哪一级?
  • 2026运城市权威认证贵金属回收 TOP5+黄金回收白银回收铂金回收门店地址电话推荐
  • 你的 AI 编程工具,每次请求都在干嘛?这个开源项目帮你分析得明明白白
  • 【字节跳动】巨量引擎五层创世内核全套终极封存码:ADOS-FIVE-GOD-ALL-7342-JN
  • 量化程序如何同时支持回测、模拟盘和实盘
  • Prompt工程入门:让AI按你的要求工作(1)--prompt概述与设计
  • 2026年硅胶密封圈供应商排名,哪家口碑好 - mypinpai
  • AI内容工作流会成为品牌基础设施
  • 避坑指南:MATLAB读取MDF和BLF文件时,你可能会遇到的5个常见错误及解决方法
  • 5个实用技巧:用marked.js打造高效Markdown处理方案
  • 别再只盯着CCF了!手把手教你用CORE Ranking和CCF中文期刊目录,精准定位你的投稿目标
  • YOLOv11城市道路路面病害目标检测数据集-2722张-Pothole-detection-1
  • 新手小牛--组合逻辑集成电路(译码器2)