一、枚举定义
- 在C++中,枚举(Enum)是一种用户定义的数据类型,用于定义命名的整数常量集合。枚举通过列出所有可能的值来创建一个新的数据类型,并为每个值分配一个整数值
#include <iostream>
enum Day {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday = 6,
Saturday
};
int main() {
Day today = Wednesday;
std::cout << today << std::endl;
if (today == Wednesday) {
std::cout << "It's Wednesday!" << std::endl;
}
return 0;
}
二、枚举类型以及对应性质
2.1 enum 枚举
enum Buffers {
Color,
Depth
};
Buffers bufferType = Color;
Buffers bufferType = Buffers(0);
int value = bufferType;
cout << bufferType;
2.2 enum class枚举
enum class Buffers {
Color,
Depth
};
Buffers bufferType = Buffers::Color;
Buffers bufferType = Color;
Buffers bufferType = Buffers(0);
int value = bufferType;
Buffers bufferType = Buffers::Color;
cast_static<int> bufferTupe;
cout << bufferType;
cout << static_cast<int>(bufferType);