具有"共同"标题的实践

Ano*_*ous 10 c++ header

通常我不是指实用程序,我的意思是包含多个类型想要使用的枚举的标题,等等.

例如,如果多个类型可以有一个Color枚举,那么您可以使其可用.有些人会说它把它"放在最适合的"类中,但是这会产生头依赖性问题.

我真的不喜欢创建一个包含这样的东西的标题,因为它似乎使代码更复杂.我正在寻找其他人在遇到这样的情况时所采用的技术的想法.如果他们使用"Common"标题等

Tho*_*ini 9

我总是使用Common.h几乎永远不会更改的文件,并包含几乎所有文件中极可能需要的定义.我认为它可以提高工作效率,因此您不必打开另一个.cpp文件并复制您知道自己肯定需要的所有标题的列表.

例如,以下是我的Common.h的两个摘录:

typedef unsigned char uint8;
typedef signed char int8;
typedef unsigned char uint08;
typedef signed char int08;
typedef unsigned short uint16;
typedef signed short int16;
typedef unsigned int uint32;
typedef signed int int32;
typedef unsigned long long uint64;
typedef signed long long int64;
typedef const char cchar;
typedef const bool cbool;
typedef char Byte;
Run Code Online (Sandbox Code Playgroud)


#ifdef ASSERT
/* Re-defining assert */
#undef ASSERT
#endif

#ifdef DEBUG
#ifndef ASSERTIONS
#define ASSERTIONS
#endif
#endif

#define ASSERT_ALWAYS(Expression)   if (!(Expression)) FatalError(ErrorInfo("Assertion Failure", #Expression, FUNCTION_NAME, __FILE__, __LINE__))

#ifdef ASSERTIONS
#ifdef DEBUG
#define ASSERT(Expression)   ASSERT_ALWAYS(Expression)
#else
#define ASSERT(Expression)   if (!(Expression)) ErrorLog("[Release Assertions]: The following assertion failed: " # Expression)
#endif
#else
#define ASSERT(Expression)
#endif
Run Code Online (Sandbox Code Playgroud)

  • 是的,<boost/cstdint.hpp>或<stdint.h>(取决于您的平台,第三方代码策略)比尝试使用您自己的typedef要好得多. (3认同)

Zep*_*ock 5

只要只有少数人在您的项目上工作,公共标题就可以了.一旦有20多人编辑该文件并来回合并更改,您就会开始噩梦.

也许另一种方法是拥有一个color.h或一个common/color.h文件,这将强制你的文件的一些结构.

  • 为什么?它使您的数据保持独立,易于查看和修改. (3认同)