Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When we have,

struct E { enum E_ { HELLO }; }; // 'E' is inheritable

then why do we need,

enum class E { HELLO };  // 'E' is not inheritable

IMO 2nd version doesn't offer more features than the 1st. I don't think that enum class is introduced just to save 2 curly braces {};! Am I missing any important aspect ?

As a minor question, is there any difference between enum class and enum struct other than syntax (because both have public access specifier) ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

Besides what was already mentioned, an advantage of enum class is better type safety - the enum class enumerators don't convert implicitly to integers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...