Enum Usage Note

Source: Internet
Author: User

Enum is a kind of structure that everyone prefers to use. It helps to provide clear logic and a bit of skill can make it more useful.

Enum em <br/>{< br/> A = 0, <br/> B, <br/> C, </P> <p> none = 0 xffffffff <br/>} 

 

This is often seen in the pastCodeI don't know how to use its final none. It's just a wonderful skill today.

Generally, using int I = B; is certainly okay. Obviously, here I is equal to 1, but if I want to convert int to Enum, in typical cases, I use Enum as the key of MAP:

Map <em, string> MP;

In this way, I need to obtain the string based on the key, but this EM may not appear in the form of em. For scalability, it is likely that you have an int I variable in your hand, requires MP. find (I); to find, so this problem occurs, how to convert int to Enum?

The reason why Enum is called Enum is nothing more than to save memory and speed up, it is very likely that if an Enum contains only 10 or 20 elements, then one byte is enough to store it. At this time, you can use an int to convert it to Enum, which is very troublesome. You may think that if an int value is loaded in one byte, then:

Em E = (byte) I;

This should be done, but the actual situation is obviously more complicated. You cannot guarantee whether a feasible scheme is passed on other compilers. After all, the size of Enum is not as predictable as you can (depending on the compiler and the content represented by Enum ).

However, we can manually maintain it in a stable memory size state. By using the above method, the final none is a number like this, 0 xffffff, this is the largest unsigned number. Generally, we use Enum to start from 0, which indicates the size of the entire unsigned number. Therefore, this Enum is basically a 4-Byte object, but it will not be smaller than 4 bytes. As for the case larger than, I did not encounter it.

Then, you can use the following method:

Em E = (EM) (DWORD) I );

To convert int I to Em, it is necessary to convert I to (DWORD), considering that the two represent the same range of numbers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.