课程: Complete Guide to C++ Programming Foundations

免费学习该课程!

今天就开通帐号,24,100 门业界名师课程任您挑!

Enumerations

Enumerations

- [Instructor] Now it's time to introduce the concept of enumerations. In C++, an enumeration or enum is a convenient way of defining a set of named integer constants. It is declared as a list of identifiers known as enumerators, which are assigned consecutive integer values starting from zero. That's why it's called an enumeration. Now, enumerations of this type were inherited from the C programming language, so they weren't really designed with object oriented programming in mind, and that makes them inappropriate for C++ code in most cases. One such problem is that C-style enumerators exist in the global scope, which may lead to naming conflicts or namespace pollution. Another problem is that C-style enums lack strong type checking, allowing implicit conversion between enums and integers, which is a safety hazard. A much better alternative that was introduced in the C++ 11 standard are enum classes. These are also known as scoped enums or strongly typed enums. Enum classes…

内容