Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Visual-Studio
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 25-Sep-12 1:03am
Comments
Legor at 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 at 25-Sep-12 10:38am
   
I agree. This is good article. And there is very well explained the difference.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 7,448
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100