当前位置: 首页 > 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.zskr.cn/news/1463644.html

相关文章:

  • 2026年大客户营销咨询选购指南,品牌排名 - mypinpai
  • PPTist:5分钟打造专业演示文稿的终极免费在线PPT制作工具
  • Mac窗口置顶神器Topit:如何让重要窗口永远在最前方
  • 紧急预警:标注数据漂移正 silently 毁掉你的模型效果!——用AI工具构建动态标注质量监控仪表盘(Python+Prometheus实战)
  • 2026年酒泉驾考驾校价格比较:新亿阳驾校性价比高吗? - mypinpai
  • 教育AI整合进入“深水区”:2024Q2行业报告显示,仅17%机构实现L1-L4能力跃迁——你的团队处在哪一级?
  • AI内容工作流会成为品牌基础设施
  • 量化程序如何同时支持回测、模拟盘和实盘
  • 避坑指南:MATLAB读取MDF和BLF文件时,你可能会遇到的5个常见错误及解决方法
  • 5个实用技巧:用marked.js打造高效Markdown处理方案
  • 别再只盯着CCF了!手把手教你用CORE Ranking和CCF中文期刊目录,精准定位你的投稿目标
  • 训练Mask-RCNN时,那个神秘的events文件怎么用TensorBoard打开看损失曲线?
  • Moneta Markets亿汇:“量子芯片点燃科技预期”
  • 如何免费实现游戏控制器虚拟化:ViGEmBus驱动完整指南
  • 手把手教你用STM32F072C8T6自制一个带串口的J-Link OB(附全套资料)
  • 为什么有些影视网站越用越顺手?一次实际体验后的分析
  • MatAnyone:一键实现专业级视频抠图的终极解决方案
  • 2026年现阶段,四川优质水果基地如何选?这份深度指南为您解析 - 2026年企业资讯
  • Aegisub字幕编辑高效解决方案:4大使用场景的完整技术指南
  • POP3协议抓包实战:从Wireshark过滤器技巧到常见认证失败排查
  • 3分钟掌握Windows窗口置顶技巧:告别频繁切换,工作效率提升50%
  • 终极指南:3分钟用BetterNCM Installer让网易云音乐焕然一新
  • 夹克制作全流程科普:工艺标准、自动化改造与设备科学选型
  • VTJ.PRO 双版本升级:构建企业级 AI 低代码协同开发新范式
  • NVIDIA Profile Inspector深度解析:显卡性能调优实战指南
  • 088、文字检测 YOLO 风格:用 YOLO 做场景文字检测替代 DBNet 的实验
  • 别再只用Measure Inertia了!用CATIA VBA脚本一键生成零件最小材料包络盒(附完整代码)
  • DDD-016:分层架构与 DDD
  • 2026玉溪市权威认证贵金属回收 TOP5+黄金回收白银回收铂金回收门店地址电话推荐
  • 做课件找不到合适BGM?11个优质课件背景音乐站点整理