【转】C/C++使用心得:enum与int的相互转换
https://blog.csdn.net/lihao21/article/details/6825722
如何正确理解enum类型?
例如:
- enum Color { red, white, blue};
- Color x;
我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将其理解成int类型。
我们再看enumeration类型:
- 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:
- enum Color { red, white, blue };
- void f()
- {
- int n;
- n = red; // change n to 0
- n = white; // change n to 1
- n = blue; // change n to 2
- }
以下代码也说明了Color会自动转换成int:
- void f()
- {
- Color x = red;
- Color y = white;
- Color z = blue;
- int n;
- n = x; // change n to 0
- n = y; // change n to 1
- n = z; // change n to 2
- }
但是,C++编译器并不提供从int转换成Color的自动转换:
- void f()
- {
- Color x;
- x = blue; // change x to blue
- x = 2; // compile-time error: can't convert int to Color
- }
若你真的要从int转换成Color,应提供强制类型转换:
- void f()
- {
- Color x;
- x = red; // change x to red
- x = Color(1); // change x to white
- x = Color(2); // change x to blue
- x = 2; // compile-time error: can't convert int to Color
- }
但你应保证从int转换来的Color类型有意义。
参考链接:点击打开链接
【转】C/C++使用心得:enum与int的相互转换的更多相关文章
- C/C++使用心得:enum与int的相互转换
如何正确理解enum类型? 例如: enum Color { red, white, blue}; Color x; 我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将 ...
- enum和int、string的转换操作
enum Countries{ 中国 = 5, 美国, 俄罗斯, 英国, 法国} enum 和 int enum -> intint num = (int)Coun ...
- C# enum、int、string三种类型互相转换
enum.int.string三种类型之间的互转 #代码: public enum Sex { Man=, Woman= } public static void enumConvert() { in ...
- 总是容易忘记:enum、int、string之间的快速转换
public enum Color { Red=, Green= } (1)Enum转换为String Color.Read.ToString() Convert.ToString(Color.Gre ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- C++语法小记---string和int的相互转换
string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...
- 用枚举enum替代int常量
枚举的好处: 1. 类型安全性 2.使用方便性 public class EnumDemo { enum Color{ RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN ...
- 第30条:用enum代替int常量
在java1.5之前,表示枚举类型的常用模式是声明一组具名的int常量,每个类型成员一个常量: public static final int APPLE_FUJI = 0; public stati ...
- (转载)C#:Enum、Int和String的互相转换,枚举转换
Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...
随机推荐
- request 发送多层字典
a. 客户端向服务端发送多层字典的值 #客户端发送 obj = { 'data':{ "k1":"v1", "k2":"v2&qu ...
- JavaScript的string方法(demo)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- nodejs while-loop
node-while-loop A while loop alternative for Nodejs based on promises. Install $ npm install --save ...
- 使用Nightwatch.js做基于浏览器的web应用自动测试
1 安装 1.1 安装Node.js 在http://nodejs.org/ 上下载适合本机系统的安装包运行安装,注意安装选项中选择npm tool以用于后续依赖包的安装. 1.2 ...
- UDIMM、RDIMM、SODIMM以及LRDIMM的区别
DIMM简介 DIMM(Dual Inline Memory Module,双列直插内存模块)与SIMM(single in-line memory module,单边接触内存模组)相当类似,不同的只 ...
- mini2440使用jlink烧写superboot到norflash
Jlink版本号:J-flash ARM V4.12 J-Flash ARM的配置. 一般说来file-->open project里面会找到一些*.jfl ...
- caffe配置Makefile.config----ubuntu16.04--重点是matlab的编译
来源: http://blog.csdn.net/daaikuaichuan/article/details/61414219 配置Makefile.config(参考:http://blog.csd ...
- VMware Workstation 11 安装MAC OS X 10.10 Yosemite(14B25)图解 2015-01-13 12:26:01|
VMware Workstation 11 安装MAC OS X 10.10 Yosemite(14B25)图解 2015-01-13 12:26:01| 分类: 网络互联 | 标签:10.10 ...
- IOS UIScrollView滚动到指定位置
[mScrollView setContentOffset:CGPointMake(0,200) animated:YES];
- 脱了裤子放屁之std::string
一个天天跟c#奋斗的苦逼c++程序猿 改自己曾经代码的时候发现有例如以下几行. char szPath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szPath, ...