我发现 C++ 中的枚举有问题。我想知道这是否是一个已知问题。
#include <iostream>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
enum color { red = 8, green = 7, blue };
int main(int argc, char** argv)
{
color r = red, g = green, b = blue;
cout << r << " " << g<< " " << b << " " <<endl;
switch (b) {
case red:
cout << "a bad thing happened" << endl;
break;
}
return 0;
}
运行你得到的程序:
8 7 8 坏事发生了
最佳答案
If the first enumerator does not have an initializer, the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one.
所以基本上蓝色是绿色 +1 => 8 等于红色 (8)
关于c++ - 与c++中枚举相关的问题。这已经是一个已知问题了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57026675/