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

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

c++ - Do we really need "enum class" in C++11?

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...