以下是代码:
#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
所以char *
打印成逗号,为什么?
答案 0 :(得分:3)
Red
为'*'
,Yellow
为'*' + 2
,即','
。
更具体地说,42是'*'
的ASCII值,44是','
的ASCII值,Red
和Yellow
相差2。
答案 1 :(得分:0)