当前位置:网站首页>Details of enum enumeration in C and C + + 11

Details of enum enumeration in C and C + + 11

2020-12-08 12:40:32 itread01

author : Li Chungang It comes from :https://www.cnblogs.com/lcgbk/p/14101271.html [toc] # One 、 Preface Because of C++ A relatively large number of enumerations are used in the project (enum), Under normal circumstances , Enumerating variables take up the size of an integer type , But the project lists (enum) Just use the size of one byte , Because it's code that runs on an embedded device , There are relatively few resources , Then if the list is based on int If the size of the model is used , This is undoubtedly a waste of resources . So I wonder if there is a way to control enumeration (enum) The way to take up memory . Fortunately , By searching for information , Find out C++11 The new feature of the new feature just adds a mechanism to control the enumeration size . So let's take a look at , Enumeration (enum) stay C++11 What are the changes in standards ? as well as C and C++11 The list in (enum) What's the difference ? # Two 、C The list in (enum) # 2.1 C The sizes listed in I said before , By default , Enumeration enum The size of is the size of a shaper type , But there are many types of plastic surgery :int、long int、short int etc. , therefore enum We can't determine the size of . stay C in enum The size of is not explicitly controlled by the programmer , This size is listed by the compiler according to what we have given (enum) The size of the assignment is used to select the appropriate integer type . Here's an example : ```cpp /***************************************************************************** ** Copyright 2020 lcg. All rights reserved. ** File name: enum.c ** Description: Test enum Characteristics of ** Author: lcg ** Version: 1.0 ** Date: 2020.12.08 *****************************************************************************/ #include enum color1 { RED = 0, GREEN, BLUE }; enum color2 { GRAY = 0x1122334455, YELLOW, PURPLE }; int main(int argc, char *argv[]) { printf("enum color1: %d\n", sizeof(enum color1)); printf("enum color2: %d\n", sizeof(enum color2)); return 0; } ``` Output results : ``` enum color1: 4 enum color2: 8 ``` We find that the size of these two enumeration variables is different ,enum color2 It has surpassed 4 Bytes , So the compiler will adjust the size of the enumeration variable according to the actual value . There is a drawback to this , When we define a structure with enumerated variables , When the enumeration variable changes , Then the size of the structure may change with it . Heap space is applied for a structure without assigning a value to the enumeration , If you give the list a value greater than 4 The value of bytes , Then the actual size of the structure has changed , In this way, there will be hidden dangers when our later program operates on the structure . ## 2.2 C The range of values listed in We continue to use the above routine to illustrate the value range : ```cpp /***************************************************************************** ** Copyright 2020 lcg. All rights reserved. ** File name: enum.c ** Description: Test enum Characteristics of ** Author: lcg ** Version: 1.1 ** Date: 2020.12.08 *****************************************************************************/ #include enum color1 { RED = 0, GREEN, BLUE }; enum color2 { GRAY = 1, YELLOW, PURPLE }; int main(int argc, char *argv[]) { enum color1 myClolor1; myClolor1 = RED; printf("myClolor1: %d\n", myClolor1); myClolor1 = GRAY; printf("myClolor1: %d\n", myClolor1); myClolor1 = 10; printf("myClolor1: %d\n", myClolor1); return 0; } ``` Output results : ``` myClolor1: 0 myClolor1: 1 myClolor1: 10 ``` You can see , stay C Next enum Defined variables can be assigned values outside the specified range , But in C++11 This is not allowed . stay C++11 The compiler will not pass , yes C++11 Yes C in enum Optimization of . # 3、 ... and 、C++11 The list in (enum) ## 3.1 c++11 Added enumeration size control in stay C++11 Programmers can list (enum) To explicitly control the amount of memory it occupies , The following example : ```cpp /***************************************************************************** ** Copyright 2020 lcg. All rights reserved. ** File name: enum.cpp ** Description: Test enum Characteristics of ** Author: lcg ** Version: 1.2 ** Date: 2020.12.08 *****************************************************************************/ #include enum color1 : char { RED = 0, GREEN, BLUE }; enum color2 : int { GRAY = 1, YELLOW, PURPLE }; int main(int argc, char *argv[]) { printf("enum color1: %d\n", sizeof(enum color1)); printf("enum color2: %d\n", sizeof(enum color2)); return 0; } ``` Output results : ``` enum color1: 1 enum color2: 4 ``` C++11 This feature can solve the problems I mentioned in the preface , It can save resources . ## 3.2 c++11 Add a scoped enumeration type Like above 2.2 If the program is in C++11 The following errors will appear in the compilation of the standard : ```cpp 1.cpp:33:15: error: cannot convert ‘color2’ to ‘color1’ in assignment myClolor1 = GRAY; ^ 1.cpp:36:15: error: invalid conversion from ‘int’ to ‘color1’ [-fpermissive] myClolor1 = 10; ``` stay C++11 In the standard ,enum A defined variable cannot be assigned a value outside the specified range . This brings a lot of convenience for later debugging

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208123938745v.html