Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition and Application of enumerated types of C++

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "the definition and application of enumerated types of C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The definition format of enumerated types is:

Enum {}

Format description:

The keyword enum-- indicates that the identifier that follows is the name of an enumerated type.

Enumeration constant table-consists of enumeration constants. "enumeration constant" or "enumeration member" is an integer in the form of an identifier that represents the value of an enumerated type. The enumeration constant table lists all values of enumerated types, separated by "," constants, and must be different. The value type is the same as the conditional expression.

Application examples:

Enumcolor_set1 {RED,BLUE,WHITE,BLACK}; / / defines the enumeration type color_set1

Enumweek {Sun,Mon,Tue,Wed,Thu,Fri,Sat}; / / defines the enumeration type week

Important tips:

Enumeration constants represent the possible values of variables of this enumeration type, and the compiler assigns an integer value to each enumeration constant. By default, this integer is the sequence number of the enumerated element, starting with 0. You can specify integer values for some or all of the enumeration constants when defining the enumeration type, the enumeration constants before the specified values are still valued by default, and the enumeration constants after the specified values are valued according to the principle of adding 1 in turn. The values of each enumeration constant can be repeated. For example:

Enumfruit_set {apple,orange,banana=1,peach,grape}

/ / enumerate the constant apple=0,orange=1,banana=1,peach=2,grape=3.

Enumweek {Sun=7,Mon=1,Tue,Wed,Thu,Fri,Sat}

/ / the values of the enumeration constant Sun,Mon,Tue,Wed,Thu,Fri,Sat are 7, 1, 2, 3, 4, 5 and 6, respectively.

Enumeration constants can only be expressed in the form of identifiers, not literal constants such as integers or characters. For example, the following definition is illegal:

Enumletter_set {'an enumeration constant cannot be a character constant.

Enumyear_set {2000, 2001, 2002, 2003, 2004, 2005}; / / enumeration constants cannot be integer constants.

It can be changed into the following form to define it legally:

Enumletter_set {a,d,F,s,T}

Enumyear_set {y2000,y2001,y2002,y2003,y2004,y2005}

This is the end of the definition and application of C++ 's enumerated types. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 286

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report