Click here to Skip to main content
14,698,142 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello friends,
I am a beginner in C++ programming. Today I come accross a new topic: strongly typed enum.I found it in below link.
http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html[^]
Then I have googled a lot. But till now I am unable to find out why do we need this? And what is the use of the same?

Let see if,
enum xyz{a, b, c};
/*a = 0, b = 1, c = 2, (Typical C format)*/
Why do we need to write
enum class xyz{a, b, c};

What are we trying to do here?

My most important doubt is how to use it.
Could you pls provide a small example, which will make me understand.

Thanks in advance
Posted
Comments
Legor 25-Sep-12 6:10am
   
I think the article you posted allready explains it perfectly and it also comes with examples.

Btw. thanks for the article ;)
armagedescu 25-Sep-12 10:38am
   
I agree. This is good article. And there is very well explained the difference.

1 solution

Traditional enums have several problems:
1. SCOPE: Enumerations export their enumerators to the surrounding scope. This has two drawbacks. First, it can lead to name clashes, if two enumerators in different enums declared in the same scope have the same name; second, it's not possible to use an enumerator with a fully qualified name, including the enum name.
2. UNDERLYING TYPE: It is not possible to specify the underlying type. It is implementation specific, but it has to be an integral type; it should not be larger than int unless the enumerator value cannot fit an int or unsigned int.
3. CONVERSION TO INT: The values of enumerators implicitly convert to int.

The strongly typed enums do not export their enumerators to the surrounding scope, can have user specified underlying type of an integral type (also added for tranditional enums) and do not convert implicitly to int.

More info you can get here: http://www.codeguru.com/cpp/cpp/article.php/c19083/C-2011-Stronglytyped-Enums.htm[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900