https://blog.csdn.net/lihao21/article/details/6825722

如何正确理解enum类型?

例如:

  1. enum Color { red, white, blue};
  2. Color x;

我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将其理解成int类型。

我们再看enumeration类型:

  1. enum Color { red, white, blue};

(C程序员尤其要注意!)
理解此类型的最好的方法是将这个类型的值看成是red, white和blue,而不是简单将看成int值。
C++编译器提供了Color到int类型的转换,上面的red, white和blue的值即为0,1,2,但是,你不应简单将blue看成是2。blue是Color类型的,可以自动转换成2,但对于C++编译器来说,并不存在int到Color的自动转换!(C编译则提供了这个转换)

例如以下代码说明了Color会自动转换成int:

  1. enum Color { red, white, blue };
  2. void f()
  3. {
  4. int n;
  5. n = red;    // change n to 0
  6. n = white;  // change n to 1
  7. n = blue;   // change n to 2
  8. }

以下代码也说明了Color会自动转换成int:

  1. void f()
  2. {
  3. Color x = red;
  4. Color y = white;
  5. Color z = blue;
  6. int n;
  7. n = x;   // change n to 0
  8. n = y;   // change n to 1
  9. n = z;   // change n to 2
  10. }

但是,C++编译器并不提供从int转换成Color的自动转换:

  1. void f()
  2. {
  3. Color x;
  4. x = blue;  // change x to blue
  5. x = 2;     // compile-time error: can't convert int to Color
  6. }

若你真的要从int转换成Color,应提供强制类型转换:

  1. void f()
  2. {
  3. Color x;
  4. x = red;      // change x to red
  5. x = Color(1); // change x to white
  6. x = Color(2); // change x to blue
  7. x = 2;        // compile-time error: can't convert int to Color
  8. }

但你应保证从int转换来的Color类型有意义。

参考链接:点击打开链接

【转】C/C++使用心得:enum与int的相互转换的更多相关文章

  1. C/C++使用心得:enum与int的相互转换

    如何正确理解enum类型? 例如: enum Color { red, white, blue}; Color x; 我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将 ...

  2. enum和int、string的转换操作

    enum Countries{    中国 = 5,    美国,    俄罗斯,    英国,    法国} enum 和 int enum -> intint num = (int)Coun ...

  3. C# enum、int、string三种类型互相转换

    enum.int.string三种类型之间的互转 #代码: public enum Sex { Man=, Woman= } public static void enumConvert() { in ...

  4. 总是容易忘记:enum、int、string之间的快速转换

    public enum Color { Red=, Green= } (1)Enum转换为String Color.Read.ToString() Convert.ToString(Color.Gre ...

  5. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  6. C++语法小记---string和int的相互转换

    string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...

  7. 用枚举enum替代int常量

    枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...

  8. 第30条:用enum代替int常量

    在java1.5之前,表示枚举类型的常用模式是声明一组具名的int常量,每个类型成员一个常量: public static final int APPLE_FUJI = 0; public stati ...

  9. (转载)C#:Enum、Int和String的互相转换,枚举转换

    Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...

随机推荐

  1. xammp 配置虚拟主机

    ## This is the main Apache HTTP server configuration file. It contains the# configuration directives ...

  2. diff patch

    http://rails-deployment.group.iteye.com/group/wiki/1318-diff-and-patch-10-minutes-guide 情景一:你正尝试从代码编 ...

  3. sublime 汇总

    此文内容有原创,还有各种其他博客抄来的经验,技巧,纯属个人使用心得. http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html ...

  4. website link

    error: template with C linkage http://blog.csdn.net/jiong_1988/article/details/7915420http://velep.c ...

  5. 简单的ftp服务器

    import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandle ...

  6. iOS移动开发周报-第20期

    iOS移动开发周报-第20期iOS移动开发周报-第20期 [摘要]:本期iOS移动开发周报带来如下内容:iOS 通知中心扩展制作入门,iOS APP可执行文件的组成,objc非主流代码技巧等. 教程 ...

  7. AngularJs 在控制器中过滤

    <html> <head> <title>Simple app</title> <script src="https://ajax.go ...

  8. JMeter中使用Put请求方式请求接口

    前言 现在有如下接口,是以PUT的方式请求的: 请求URL:IP+Port+/api/v1/apps/{appId} 请求参数: 参数名 必选 类型 nameCn 是 string nameEn 是 ...

  9. IOS8 通知中心(Notification Center)新特性

     本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/30029441   ios手机apple通知中心notificationCenter ...

  10. EasyPusher直播推送中用到的缓冲区设计和丢帧原理

    问题描述 我们在开发直播过程中,会需要用到直播推送端,推送端将直播的音视频数据推送到流媒体服务器或者cdn,再由流媒体服务器/CDN进行视频的转发和分发,提供给客户端进行观看.由于直播推送端会存在于各 ...