由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 再请教一个c++的简单问题(enumerated constant)
相关主题
typedef 的一个问题10个包子请教一个简单的编程问题
又问几个c语言编程的题目python question
水木上python大坑啊: 疑Google员工把8w行Python项目用4w行Java重写了Python做计算怎么只用一个核?
scala为什么用两个空格?python code performance --- normal or too slow?
some interview questions怎么用python把这个data读进去,变成dict结构
How to have another func call printf with va_arg list ?何用openpyxl 从excel 文档的某一行读起?
xml schema beginner question求助:关于2个python的题目
Shuffle performance (C#)python代码写成这样,什么鬼阿。
相关话题的讨论汇总
话题: enum话题: suit话题: c++话题: enumerated话题: spade
进入Programming版参与讨论
1 (共1页)
s********h
发帖数: 286
1
如果定义一个enumerated constant,比如这个:
enum {jack, diomand, heart, spade};
然后定义一个变量:
int suit = spade;
然后我输出suit的话,如果用
cout << suit;
结果就会是 3 ,可是我想让它输出的是 string “spade”,应该怎么做呢?
谢谢先!
b********r
发帖数: 1080
2
笨办法.
enum xxx{jack, diomand,...};
char* card[]={"jack", "diomand", ......};
int suit=spade;
cout<
l********s
发帖数: 358
3
Create a class called Suit which encapsulate "jack" or "diomand" or ...
Then overload operator <<
s******n
发帖数: 21
4
Does C++ 0x enum class provide any elegant solutions? C# is way better
when dealing with enumerations….

【在 l********s 的大作中提到】
: Create a class called Suit which encapsulate "jack" or "diomand" or ...
: Then overload operator <<

n******t
发帖数: 4406
5
同学,suit是一个int,里面值是3,你怎么可能叫他打印出spade???
我越来越觉得oo这个东西害人不浅了。

【在 s********h 的大作中提到】
: 如果定义一个enumerated constant,比如这个:
: enum {jack, diomand, heart, spade};
: 然后定义一个变量:
: int suit = spade;
: 然后我输出suit的话,如果用
: cout << suit;
: 结果就会是 3 ,可是我想让它输出的是 string “spade”,应该怎么做呢?
: 谢谢先!

s********h
发帖数: 286
6
多谢回复啊,可是这样做感觉就没有必要用 enumerator 了啊?
用 enumerator constant 应该是让这个更简单才对啊?

【在 l********s 的大作中提到】
: Create a class called Suit which encapsulate "jack" or "diomand" or ...
: Then overload operator <<

s********h
发帖数: 286
7
谢谢指点啊!你的方法比我自己的好:)
等一下给你发包子啊!

【在 b********r 的大作中提到】
: 笨办法.
: enum xxx{jack, diomand,...};
: char* card[]={"jack", "diomand", ......};
: int suit=spade;
: cout<
t****t
发帖数: 6806
8
i don't think so. basically he is asking some kind of reflection mechanism;
which c++ doesn't provide anyway (except using macro). neither does c++0x.

【在 s******n 的大作中提到】
: Does C++ 0x enum class provide any elegant solutions? C# is way better
: when dealing with enumerations….

t****t
发帖数: 6806
9
we are talking about enum, not arbitrary class. of course nothing is
impossible (given you can implement with anything else), but there is
difference between elegent solution and ugly solution.
w*********l
发帖数: 1337
10
越来越觉得c++自己把自己的路走绝了。以前基本是objective C的感觉。现在站在C#跟
C中间,两头都不靠。

【在 s******n 的大作中提到】
: Does C++ 0x enum class provide any elegant solutions? C# is way better
: when dealing with enumerations….

t****t
发帖数: 6806
11
can you read? i don't know C# at all, not sure what solution did i say for C
#.
Let's say we have (enum) type
enum color { R, G, B };
given an object of type color, return "R" or "G" or "B" accordingly.
This problem is quite easy, you may use a std::map or whatever you feel
comfortable.
However, when people discuss about reflection, the problem is,
if we have an arbitrary (enum) type T
enum T { T1, T2, T3, ... };
given any object of *any* enum type T, return "T1" or "T2" or "T3" or ...
accordin
s******n
发帖数: 21
12
Elegant or not, that's just personal opinion and there is no point to argue
about that. But I do think c# enum provides some very interesting features
that worth mentioning:
- Strongly typed
- Able to specify the base type, int32 will be used if no base type is
specified
- Build-in functions to convert from values to printable strings and vice
versa. Thrust has explained this in his post
- Able to define enum as a bit field.
- Since enum is a class, it can be extended.
Looks like C++ is trying
1 (共1页)
进入Programming版参与讨论
相关主题
python代码写成这样,什么鬼阿。some interview questions
能否通过父类指针来复制子类对象?How to have another func call printf with va_arg list ?
做题xml schema beginner question
请教一个Python问题, 怎么读出一个data structure中的全部members (内容)?Shuffle performance (C#)
typedef 的一个问题10个包子请教一个简单的编程问题
又问几个c语言编程的题目python question
水木上python大坑啊: 疑Google员工把8w行Python项目用4w行Java重写了Python做计算怎么只用一个核?
scala为什么用两个空格?python code performance --- normal or too slow?
相关话题的讨论汇总
话题: enum话题: suit话题: c++话题: enumerated话题: spade