当然:
#include <iostream> // 定义一个枚举类型
enum class Color { RED, GREEN, BLUE
}; // 函数返回枚举类型
Color getRandomColor() { static int nextColorIndex = 0; Color color = Color(nextColorIndex); ++nextColorIndex; if (nextColorIndex > 2) nextColorIndex = 0; // 循环使用颜色值 return color;
} int main() { for (int i = 0; i < 5; ++i) { Color randomColor = getRandomColor(); std::cout << "Random color: " << static_cast<int>(randomColor) << std::endl; } return 0;
}