Enum vs Strongly typed enum

后端 未结 5 1820
长情又很酷
长情又很酷 2020-11-29 18:44

I am a beginner in C++ programming.

Today I come across a new topic: strongly typed enum. I\'ve researched it a bit but till now I am unable to find ou

5条回答
  •  旧巷少年郎
    2020-11-29 19:12

    There's a good article about enums at this IBM page, it's very detailed and well-written. Here are some important points in a nutshell:

    The scoped enums solve most of the limitations incurred by regular enums: complete type safety, well-defined underlying type, scope issues, and forward declaration.

    • You get type safety by disallowing all implicit conversions of scoped enums to other types.
    • You get a new scope, and the enum is not anymore in the enclosing scope, saving itself from name conflicts.
    • Scoped enums gives you the ability to specify the underlying type of the enumeration, and for scoped enums, it defaults to int if you choose not to specify it.
    • Any enum with a fixed underlying type can be forward declared.

提交回复
热议问题