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接口中不支持引用,采用指针

相关推荐
啦啦啦啦啦zzzz1 小时前
数据结构:红黑树理论
数据结构·c++·红黑树
Yolo_TvT1 小时前
C++:默认构造函数
c++
小欣加油3 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
.千余4 小时前
【C++】手写双向链表:list容器模拟实现
开发语言·c++·笔记·学习·其他
liulilittle4 小时前
过冲:拥塞控制的呼吸与盲行
linux·网络·c++·tcp/ip·计算机网络·tcp·通信
小欣加油5 小时前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
weixin_461769405 小时前
通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null
数据结构·c++·算法·vector·nullptr·null
千寻girling5 小时前
一周没跑步了 ,今日跑步 5KM , 哑铃+健身 20min , 俯卧撑 30 个 ;
数据结构·c++·python·算法·leetcode·职场和发展·线性回归
坚果派·白晓明5 小时前
鸿蒙PC三方库使用:使用 AtomCode + Skills 自动完成鸿蒙化三方库spdlog集成
c++·华为·ai编程·harmonyos·skills·atomcode·c/c++三方库