c++11中引入了新的枚举类型---->强制枚举类型

// unscoped enum:
enum [identifier] [: type]
{enum-list};  // scoped enum:
enum [class|struct] [identifier] [: type]
{enum-list};
  identifier:指定给与枚举的类型名称。
  type:枚举器的基础类型(默认int),所有枚举器都具有相同的基础类型,可能是任何整型。
  enum-list:枚举中以逗号分隔的枚举器列表。 范围中的每个枚举器或变量名必须是唯一的。 但是,值可以重复。 在未区分范围的枚举中,范围是周边范围;在区分范围的枚举中,范围是 enum-list 本身。
  class:可使用声明中的此关键字指定枚举区分范围,并且必须提供 identifier。 还可使用 struct 关键字来代替 class,因为在此上下文中它们在语义上等效。

如下为两者的简单示例:
enum Test
{
  test1,
  test2
}; int a = test1;  // 类型隐式转换,枚举常量无须限定
if (test1 == 0)
  cout << "Hello world."; enum class ErrorCode
{
  ERROR_ONE,
  ERROR_TWO,
  ERROR_THREE
}; int num = 2;
num = static_cast<int>(ErrorCode::ERROR_ONE); // 类型需要显示转换,而且枚举常量必须限定
ErrorCode test = static_cast<ErrorCode>(12);  // 其实这个整数已经超出范围了,但是居然合法
if (test == ErrorCode::ERROR_THREE)
  cout << "It's impossible"

enum 与 enum class的更多相关文章

  1. C++11的enum class & enum struct和enum

    C++11的enum class & enum struct和enum C++标准文档--n2347(学习笔记) 链接:http://www.open-std.org/jtc1/sc22/wg ...

  2. [转]C++11的enum class & enum struct和enum

    1. 旧版enum存在的问题 问题 描述 1 向整形的隐式转换(Implicit conversion to an integer) 2 无法指定底层所使用的数据类型(Inability to spe ...

  3. 【转】C++11的enum class & enum struct和enum

    转自:https://blog.csdn.net/sanoseiichirou/article/details/50180533 C++标准文档——n2347(学习笔记) 链接:http://www. ...

  4. C++中typedef enum 和 enum

    在C++中,这两种定义枚举类型的关键字用法和效果相同,推荐使用前者.typedef enum多用在C语言中. 在C语言中,如果使用typedef enum定义一个枚举类型,比如: typedef en ...

  5. 关于枚举,enum、Enum、EnumSet、RegularEnumSet、JumboEnumSet

    Apache Commons Lang. 在版本3中,enum相关的工具就留下EnumUtils. 首先, 所有enum,都默认实现了抽象类 java.lang.Enum .所以,所有enum都具备E ...

  6. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  7. 关于Java中枚举Enum的深入剖析

    在编程语言中我们,都会接触到枚举类型,通常我们进行有穷的列举来实现一些限定.Java也不例外.Java中的枚举类型为Enum,本文将对枚举进行一些比较深入的剖析. 什么是Enum Enum是自Java ...

  8. MYSQL中 ENUM 类型

    MYSQL中 ENUM 类型的详细解释 ENUM类型 ENUM 是一个字符串对象,其值通常选自一个允许值列表中,该列表在表创建时的列规格说明中被明确地列举. 在下列某些情况下,值也可以是空串(&quo ...

  9. Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum

    Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个 ...

随机推荐

  1. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  2. 使用markdown

    一.在windows下使用markdown MarkdownPad:MarkdownPad is a full-featured markdown editor for windows. Awsomi ...

  3. CentOS配置git和maven自动部署java

    #安装Git yum install git #测试是否成功 git -version #正确 #git version 1.7.1 #配置git config --global user.name ...

  4. Socket编程实践(3) 多连接服务器实现与简单P2P聊天程序例程

    SO_REUSEADDR选项 在上一篇文章的最后我们贴出了一个简单的C/S通信的例程.在该例程序中,使用"Ctrl+c"结束通信后,服务器是无法立即重启的,如果尝试重启服务器,将被 ...

  5. Git 耍不起来啊

    1.  $ git clone https://******.git Cloning into 'dt-engine-server'...fatal: unable to access 'https: ...

  6. Data Binding使用技巧

    Data Binding 根据变量,自动赋值到各widget. How 1.编写layout文件,这里的layout为: act_data_bind_demo.xml 这里需要先准备变量 在具体的wi ...

  7. HDU 5944 Fxx and string(暴力/枚举)

    传送门 Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Othe ...

  8. WA题集

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  9. okhttp封装时,提示 cannot resolve method OkHttpClient setConnectTimeout() 函数

    如标题所示,okhttp封装时,提示 cannot resolve method  OkHttpClient setConnectTimeout() 函数,有遇到这样现象的朋友吗? 原因:因使用的是 ...

  10. Android SDK NDK开发总结

    描述:http://talent.baidu.com/external/baidu/index.html#/jobDetail/2/1237247043 android studio实现Jni(C/C ...