Use of enum Enum in C + + and Java

Source: Internet
Author: User

Enum enum is the keyword in both C + + and Java, but they are not the same. For C + +, enumerations are a series of named Integer constants, and conversion from enumeration values to corresponding integer values is done internally. For Java, enumerations are more like named instances of a class, and you can customize the members of an enumeration, and the value of the enumeration to the corresponding integer is then externally. The following is a blog 8.1 Implement Blackjack realized 21 points, there is a poker color enumeration class:

//C + + definationenumSuit {Club, Diamond, Heart, Spade};//Java defination Public enumSuit {Club (0), Diamond (1), Heart (2), Spade (3); Private int_value; PrivateSuit (intV) {_value =v;};  Public intGetValue () {return_value;};  Public StaticSuit Getsuitfromvalue (intValue) {};// ...};

Above is the implementation of C + + and Java implementation of color enumeration, we can see that C + + conversion process is done internally, and Java needs to write its own conversion process. For C + + enumeration, if not special assignment, then the corresponding integer number is starting from 0 and so on, if the partial assignment, from the value of the assignment, and so on, see here, the following three lines of code is also good to illustrate this point:

enum // 0, 1, 2, 3 enum 1 // 1, 2, 3, 4 enum 5 4 // 5, 6, 4, 5

For C + +, in addition to the enum keyword, there is an enum class keyword, which is an enumeration that differs from enumeration in that the enumeration name is local and does not convert internally to other types, such as Integer. The following code is a good illustration of the difference:

enumColor {red, green, blue};//Plain enumenumCard {red_card, Green_card, yellow_card};//another plain enumenum classAnimal {dog, deer, cat, bird, human};//Enum classenum classMammal {kangaroo, deer, human};//another enum classvoidFun () {//Examples of bad use of plain enums:Color color =color::red; Card Card=Card::green_card; intnum = color;//No problem    if(color = = Card::red_card)//no Problem (bad)cout <<" Bad"<<Endl; if(Card = = Color::green)//no Problem (bad)cout <<" Bad"<<Endl; //examples of good use of enum classes (SAFE)Animal A =Animal::d EER; Mammal M=mammal::d EER; intnum2 = A;//Error    if(M = = a)//error (good)cout <<" Bad"<<Endl; if(A = = mammal::d EER)//error (good)cout <<" Bad"<<Endl;}

Use of enum Enum in C + + and Java

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.