2017-09-07 96 views
0

下面是代码:C++枚举类正确

#include "stdafx.h" 
#define _USE_MATH_DEFINES 
#include <cmath> 
#include <iostream> 
#include <iomanip> 


enum Suit : long {Heart, Club, Spade, Diamond}; 
enum class Color : char {Red = '*', Blue, Yellow, Green}; 
int main(int argc, wchar_t* argv[]) 
{ 
    using namespace std; 
    auto suit = Suit::Club; 
    auto color = Color::Yellow; 
    cout << setw(37) << left << "Suit value: " << setw(5) << right << suit << endl; 
    cout << setw(37) << left << "Suit value +10: " << setw(5) << right << suit + 10 << endl; 
    cout << setw(37) << left << "Color value: " << setw(5) << right << static_cast<char>(color) << endl; 
    cout << setw(37) << left << "Color value +10: " << setw(5) << right << static_cast<int>(color) << endl; 

    wchar_t x; 
    wcin >> x; 
    return 0; 
} 

结果在vs2017运行:

Suit value:        1 
Suit value +10:       11 
Color value:        , 
Color value +10:      44 

所以焦炭*印刷为逗号,为什么呢?

+2

不,红色会显示'*',黄色显示'',因为ascii表是'* +, - ' – Petesh

+1

啊,我的坏。现在需要一杯咖啡。 – qed

回答

3

Red'*'Yellow'*' + 2,它是','

更具体地说,42是'*'的ASCII值,44为',',和RedYellow由2

0

Ascii Code Table

区别你知道如何enum工作,enum GET值的变量+1最后一个值。为前

enum { 
     sunday = 0, monday, tueday, ... , saturday 
} 

如果存取权限的monday它将1值。 ,因为您已给出red = '*'。所以编译器你的enum将看起来像这样。

enum { 
     red = '*', 
     blue = '+', 
     yellow = ',' 
} 

所以现在你知道了。