c++ - 与c++中枚举相关的问题。这已经是一个已知问题了吗?

标签 c++ enums

我发现 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 坏事发生了

最佳答案

引自cppreference.com

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/

相关文章:

c++ - 我怎样才能制作一个不断改变输入的文本框?

c - 如何比较 switch-case 中的枚举值?

java - 使用枚举而不是集合的好处

java - 比较枚举中的排名

java - 为什么我收到错误 "Constructor is undefined"?

python - 将字符串表示与使用整数值的枚举相关联?

c++ - 如果 Proactor 设计模式在异步 I/O 方面更胜一筹,为什么它在 ASIO 中不是默认的?

c++ - 对 n 维点进行排序并跟踪原始索引

c++ - typeid 何时可以为同一类型返回不同的 type_info 实例?

c++ - 程序忽略 while 循环内容,当强制访问循环内的方法时崩溃