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
所以焦炭*
印刷为逗号,为什么呢?
不,红色会显示'*',黄色显示'',因为ascii表是'* +, - ' – Petesh
啊,我的坏。现在需要一杯咖啡。 – qed