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 |