C++笔记之enum class和emun的区别
code review!
代码,使用 enum class 的示例:
#include <iostream>
enum class Month { January, February, March, April, May, June, July, August, September, October, November, December };
int main() {
Month currentMonth = Month::August;
if (currentMonth == Month::August) {
std::cout << "It's August!" << std::endl;
}
// Uncommenting the following line will result in a compilation error
// if (currentMonth == December) {
// std::cout << "It's December!" << std::endl;
// }
return 0;
}
代码,使用普通的 enum 的示例:
#include <iostream>
enum Month { January, February, March, April, May, June, July, August, September, October, November, December };
int main() {
Month currentMonth = August;
if (currentMonth == August) {
std::cout << "It's August!" << std::endl;
}
// The following line compiles successfully, but it's not type-safe
if (currentMonth == December) {
std::cout << "It's December!" << std::endl;
}
return 0;
}