What's an enum class and why should I care?

前端 未结 4 1650
谎友^
谎友^ 2021-01-12 19:21

For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me:

What is an

4条回答
  •  走了就别回头了
    2021-01-12 20:01

    1. You can explicitly specify the storage type when the data size is important (packing it in a struct perhaps).
    2. The enumeration values are scoped within the name of the enum only; before c++11 they leaked into the enclosing scope.
    3. I seem to recall the conversion rules were also changed somewhat...

    In relation to point one 1, the storage size of enums would change before C++11 depending on the largest value assigned to an enumeration. Usually it doesn't matter so much, but when it does you have to resort to ugly hacks to force the size.

    As for point 3, in C++11 enums are not implicitly convertible or comparable to ints or other enum types: useful for avoiding function overloading headaches and other implicit conversion gotchas.

提交回复
热议问题