c++ - 具有 "Common" header 的实践

标签 c++ header

<分区>

我所说的通用不是指实用程序,而是指包含多个类型想要使用的枚举的 header 等。

例如,如果多个类型可以有一个 Color,它是一个枚举,您需要使它可用。有些人会说将它放入它“最适合”的类中,但这会产生 header 依赖性问题。

我真的不喜欢创建包含此类内容的 header ,因为它似乎会使代码更加复杂。我正在寻找其他人对他们遇到这种情况时采用的技术的想法。如果他们使用“通用” header 等。

最佳答案

我总是使用几乎从不更改的 Common.h 文件,它包含几乎所有文件中极有可能需要的定义。我认为它可以提高工作效率,这样您就不必打开另一个 .cpp 文件并复制您肯定需要的所有 header 的列表。

例如,这是我的 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;


#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

关于c++ - 具有 "Common" header 的实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1950003/

相关文章:

c++ - 为链表实现选择排序

c++ - 转发声明一个标准容器?

C++ - Zlib - header 和无 header 支持。有多可靠?

c++ - 在 header 中声明对象

c++ - boolean 类型操作

c++ - 在 QT5 中使用 QProcess 运行 gcc

c++ - C++ 中的 HTTP 代理服务器

c++ - 错误 C4839 : non-standard use of class 'ATL::CW2AEX<520>' as an argument to a variadic function

javascript - 如何使用 header 在 PHP 中打开新的两个选项卡

c++ - 在 QT GUI 应用程序中切换窗口