Enum in C/C ++)

Source: Internet
Author: User

If a variable requires several possible values, it can be defined as an enumeration type. The reason for enumeration is to enumerate the possible values of variables or objects.

For example, to make everyone better understand that there is a pen in a pencil, but you don't know what it is before you open it, it may be a pencil or a pen. There are two possibilities, so you can define an enumeration type to represent it!

Enum box {penpencil, pen}; // here you define a variable of Enumeration type called box, the enumerated variable contains two elements, the pencil and pen, respectively.

Here, if you want to define two variables with the same Enumeration type, you can use the following two methods to define them!

Enum box {penpencil, pen };

Enum box box2; // or abbreviated to box box2;

Another way is to define the statement at the same time.

Enum {penpencil, pen} box, box2; // define the statement at the same time!

The enumeration Element System in enumeration variables is processed according to constants, so it is called enumeration constants. They cannot assign values to ordinary arithmetic values (penpencil = 1 ;) this is an error in writing and sending, but you can assign values when declaring it!

Enum box {penpencil = 1, pen = 2 };

However, if you do not assign values to elements, the system automatically increments the values from 0, if you only define the first element, the system adds 1 to the value of the previous element to the next element. For example

Enum box {penpencil = 3, pen}; // here pen is 4. The system will automatically assign values to pen = 4!

 
The enumeration type in C ++ inherits from the C language. Just like many other features inherited from the C language, the C ++ enumeration also has disadvantages. The most significant one is the scope problem-a constant defined in the enumeration type, it belongs to the scope of the defined enumeration, and does not belong to this enumeration type. For example:

Enum fileaccess {
Read = 0x1,
Write = 0x2,
};

Fileaccess access =: read; // correct
Fileaccess access = fileaccess: read; // Error

This feature of C ++ enumeration is unacceptable for those who are used to object-oriented and scope concepts. First, fileaccess: Read is obviously more in line with the programmer's intuition, because the enumeration definition above should be equivalent to the following definition (in fact, the enumeration type in. NET is implemented in this way ):

Class fileaccess {
Static const int READ = 0x1;
Static const int write = 0x2;
};

Secondly, we cannot define two enumeration values with the same name in the same scope. That is to say, the following code is a compilation error:

Enum fileaccess {
Read = 0x1,
Write = 0x2,
};

Enum fileshare {
Read = 0x1, // redefinition
Write = 0x2, // redefines
};

If this does not make you angry, you may not have written much C ++ code :-). In fact, in the latest draft C ++ 0x standard, there are proposals on enumeration scope issues, but what the final solution will be, cannot be left blank, after all, you must be cautious when adding, deleting, or modifying any feature in a widely used language like C ++.

Of course, we can use some roundabout methods to solve this problem (C ++ can always surprise and surprise us a lot ). For example, we can place enumeration values in a structure and use Operator Overloading to approximate enumeration features:

Struct fileaccess {
Enum _ Enum {
Read = 0x1,
Write = 0x2
};
_ Enum _ value; // enumerated Value

Fileaccess (INT value = 0): _ value (_ Enum) value ){}
Fileaccess & operator = (INT value ){
This-> _ value = (_ Enum) value;
Return * this;
}
Operator int () const {
Return this-> _ value;
}
};

We can use this enumeration type as expected:

Fileaccess access = fileaccess: read;

Besides, because we provide an int-type conversion operator, we can use it wherever int is needed, such as the switch statement:

Switch (ACCESS ){
Case fileaccess: Read:
Break;
Case fileaccess: write:
Break;
}

Of course, we do not want to write such a structure manually every time. By using macros, we can easily achieve this:

# Define declare_enum (e )/
Struct E/
{/
Public :/
E (INT value = 0): _ value (_ Enum) value ){/
}/
E & operator = (INT value ){/
This-> _ value = (_ Enum) value ;/
Return * This ;/
}/
Operator int () const {/
Return this-> _ value ;/
}/
/
Enum _ Enum {

# Define end_enum ()/
};/
/
PRIVATE :/
_ Enum _ value ;/
};

We can now define the previous enumeration as follows, and it is no more complicated than writing Enum directly.

Declare_enum (fileaccess)
Read = 0x1,
Write = 0x2,
End_enum ()

Declare_enum (fileshare)
Read = 0x1,
Write = 0x2,
End_enum ()
 
 

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.