gpt4 book ai didi

c++ - 遍历常量枚举#define

转载 作者:行者123 更新时间:2023-11-28 06:26:28 25 4
gpt4 key购买 nike

我的 C++ 代码中有一个 const 枚举,我想知道我是否可以循环遍历这些颜色,例如对这个枚举的每个成员的整数引用

const enum Colors
{
#define WHITE(alpha) D3DCOLOR_ARGB(alpha, 255, 255, 255)//
#define RED(alpha) D3DCOLOR_ARGB(alpha, 255, 000, 000)//
#define GREEN(alpha) D3DCOLOR_ARGB(alpha, 000, 255, 000)//
#define BLUE(alpha) D3DCOLOR_ARGB(alpha, 000, 000, 255)//
#define BLACK(alpha) D3DCOLOR_ARGB(alpha, 000, 000, 000)//
#define PURPLE(alpha) D3DCOLOR_ARGB(alpha, 125, 000, 255)//
#define GREY(alpha) D3DCOLOR_ARGB(alpha, 44,44, 46) //
#define YELLOW(alpha) D3DCOLOR_ARGB(alpha, 255, 255, 000)//
#define ORANGE(alpha) D3DCOLOR_ARGB(alpha, 255, 165, 000)//
#define DEEPSKYBLUE(alpha) D3DCOLOR_ARGB(alpha, 30, 144, 255) //
#define CHOCOLATE2(alpha) D3DCOLOR_ARGB(alpha, 238, 118, 33) //
};

此列表不完整 -> 我的枚举中有很多颜色

所以我真的想知道我是否可以使用可以循环遍历此枚举的键盘快捷键循环遍历所有颜色...

#define WHITE(alpha)            D3DCOLOR_ARGB(alpha, 255, 255, 255) = 1//<< something like that

我试过了,但这是不可能的......

最佳答案

首先,你不应该混合使用 #define 宏和 enum - 它们是完全不同的东西,你的代码等于 -

// Why was there `const`..?
enum Colors
{
/* empty enum */
};

#define WHITE(alpha) D3DCOLOR_ARGB(alpha, 255, 255, 255)//
#define RED(alpha) D3DCOLOR_ARGB(alpha, 255, 000, 000)//
#define GREEN(alpha) D3DCOLOR_ARGB(alpha, 000, 255, 000)//
#define BLUE(alpha) D3DCOLOR_ARGB(alpha, 000, 000, 255)//
#define BLACK(alpha) D3DCOLOR_ARGB(alpha, 000, 000, 000)//
#define PURPLE(alpha) D3DCOLOR_ARGB(alpha, 125, 000, 255)//
#define GREY(alpha) D3DCOLOR_ARGB(alpha, 44,44, 46) //
#define YELLOW(alpha) D3DCOLOR_ARGB(alpha, 255, 255, 000)//
#define ORANGE(alpha) D3DCOLOR_ARGB(alpha, 255, 165, 000)//
#define DEEPSKYBLUE(alpha) D3DCOLOR_ARGB(alpha, 30, 144, 255) //
#define CHOCOLATE2(alpha) D3DCOLOR_ARGB(alpha, 238, 118, 33) //

所以,从现在开始,我将忽略 enum Colors 并讨论那些宏。


我建议您将值存储到数组中。

COLOR_TYPE colors[] = {
WHITE(0),
RED(0),
...
};

请注意 alpha 为零。由于数组只能存储值(不能存储宏函数),因此我应该用一些东西修复 alpha

然后,你可以像这样使用它:

color[42] & (alpha << 24)

如果觉得乱,可以再做一个宏

#define GET_COLOR(n, alpha) (color[(n)] & ((alpha) << 24))

或内联函数(..推荐)

inline COLOR_TYPE GetColor(int n, uint8_t alpha)
{
return color[n] & (alpha << 24);
}

关于c++ - 遍历常量枚举#define,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com