typeid of enumerator

Mar 8, 2013 at 4:34pm
hello

i am reading about enumeration and i was trying this example using "Visual C++ 2012":
1
2
enum Suit {Clubs, Diamonds, Hearts, Spades};
cout<<typeid(Suit::Diamonds).name()<<endl;

i though that it will give me int as output but instead i received this:
?AW4Suit@?1?main@

So i tried other example:
1
2
enum  Suit {Clubs, Diamonds, Hearts, Spades};
cout<<typeid(Suit).name()<<endl;

and i even used enum class :
1
2
enum class Suit {Clubs, Diamonds, Hearts, Spades};
cout<<typeid(Suit::Diamonds).name()<<endl;

they all gave me the same output:
?AW4Suit@?1?main@

can anyone explain why, please?
Last edited on Mar 8, 2013 at 4:41pm
Mar 8, 2013 at 5:23pm
In both cases, an enum is actually a unique type, not just a fancy way to crate a bunch of integer constants. With the regular old deprecated "enum", it is implicitly convertible to and from an int, which is why in old code you often see it used that way. It isn't actually an int, but it can be converted to and from an int without explicitly casting.
Last edited on Mar 8, 2013 at 5:23pm
Mar 8, 2013 at 10:45pm
thanks L B for your reply.
Mar 8, 2013 at 10:53pm
I wonder what would happen if you did this:
1
2
3
enum Suit : char
{Clubs, Diamonds, Hearts, Spades};
cout<<typeid(Suit::Diamonds).name()<<endl;
Topic archived. No new replies allowed.