C ++: 61 --- enumerated types (enum) special tools and technologies

  • Enumerated type so that we can set integer constant organizations together . And class, each enumerated type defines a new type. Enumeration literals belong to type

First, the enumeration of classification

  • C ++ contains two enumeration:
    • Scoped (C ++ 11 Standard incorporated)
    • Not scoped

Scoped

  • Scoped general form is:
    • The first is the keyword enum class (or equivalently call enum struct)
    • Followed by a name and a list of enumerated types enumeration members separated by commas flowers bracketed
    • The last contains a semicolon
  • For example, the following definition of an enumeration type called open_modes, including three members of the enumeration:
enum class open_modes { input, output, append };

Not scoped

  • Not scoped enum type is omitted keyword class (or struct) , enumerated type name is optional
//命名的
enum color { red, yellow, green };
//未命名的
enum { floatPrec = 6, doublePrec = 10, double_doublePrec = 10 };
  • If the enum is not named, then we can only define it in the definition of the object of the enum . Similar definitions and classes, we need to provide a comma-separated list of declarations between enum defined on the right side braces and semicolon at the end

Second, the enumeration members

Scope of an enumerator

  • Two kinds of enumerated type members of different scopes:
    • Scoped enumeration type in: name of the enumeration members follow the normal scope of the guidelines, and outside is not accessible in the role of the enumeration type
    • Enumerated type is not limited in scope: enumeration members of the same scope with enumerations scope itself
  • Case presentation:
enum color { red, yellow, green };         //不限定作用域的枚举类型
//enum stoplight { red, yellow, green };   //错误,与color中的成员冲突
enum class peppers { red, yellow, green }; //正确,限定作用域的枚举成员只在花括号中有效

color eyes = green;  //正确,不限定作用域的枚举类型的枚举成员位于有效的作用域中
//peppers p = green; //错误,peppers的枚举成员不在有效作用域中,color::green在有效的作用域中,类型不一致

color hair = color::red;   //正确,允许显式地访问枚举成员
peppers p2 = peppers::red; //正确,使用peppers的red

The value of an enumerator

  • By default , the enumeration values from zero, is incremented by 1
  • If we do not explicitly provide an initial value, the current value of the enumeration enumeration members before the value of the members plus one
  • We can appoint a special value to an enumeration member, for example:
enum class intTypes {
    charTyp = 8, shortTyp = 16, intTyp = 16,
    longTyp = 32, long_longTyp = 64
};
  • Enum member is const, so the initial value provided when initializing enum member must be a constant expression . That is, each member of the enumeration itself is a constant expression, we can use the enumeration members require constant expression in any place. For example, we can define enumeration types constexpr variables:
enum class intTypes {
    charTyp = 8, shortTyp = 16, intTyp = 16,
    longTyp = 32, long_longTyp = 64
};

int main()
{
    constexpr intTypes charbits = intTypes::charTyp;
    return 0;
}
  • Enumeration of other usage scenarios:
    • We can be a enum as the expression of a switch statement, but the enumeration value as a case label
    • We will also enumerated type as a non-type template parameter use
    • Enumeration type initialized static data member in the type definition

Third, and class, define a new enumeration type

  • As long as there is enum name, we can define and initialize members of this type
  • Want to initialize the object or enum to enum the object assignment , you must use an enumeration type or another member of the objects of that type . E.g:
enum class open_modes { input, output, append };

int main()
{
    //open_modes om = 2;    //错误,2不属于类型open_modes

    open_modes om;
    om = open_modes::input; //正确,input是open_modes的一个枚举成员
    return 0;
}
  • A not scoped object or enumeration type enumeration members automatically converted to an integer . Therefore, we can use them wherever needed an integer value . E.g:
enum color { red, yellow, green };
enum class peppers { red, yellow, green };

int main()
{
    int i = color::red;   //正确,不限定作用域的枚举类型的枚举成员隐式地转换成int
    int j = peppers::red; //错误,限定作用域的枚举类型不会进行隐式转换
    return 0;
}

Fourth, specify the size of enum

  • Although each enum defines a unique type, but in fact enum is represented by an integer type
  • In the C ++ 11 standard, we can enum of a colon and the type we want to use in the name of the enum
  • E.g:
enum intValues :unsigned long long
{
    charTyp = 255, shortTyp = 65535, intTyp = 65535,
    longTyp = 4294967285UL,
    long_longTyp = 18446744073709551615ULL
};
  • By default:
    • Scoped enum member type is int
    • Enumerated type is not limited scope , its default type enumeration member does not exist, we only know the type of potential members is large enough, can certainly accommodate enumeration value
  • If we specify the type of an enumerator potential (including implicitly specified limiting the scope of the enum), then once the value of an enumerator beyond the scope of this type can be accommodated, the initiator program error
  • Enum potential capability specifying the type of control allows us to implement different types of environments , we can ensure that the environment in one implementation of the program by the compiler generated code and the code generated in other environments consistent

Fifth, enumerated types of pre-declaration

  • In the C ++ 11 standard, we can declare enum in advance . enum declaration front (whether implicitly or explicitly) must specify the size of its members
  • Format is as follows:
    • For not scoped enum is: because it is a member of the default size is not specified, each statement must specify the size of the members of the
    • For scoped enum is: may not specify the size of its members, this value is implicitly defined as int 
  • E.g:
enum intValues :unsigned long long; //前置声明,指定了成员类型
enum intValues //定义
{
    charTyp = 255, shortTyp = 65535, intTyp = 65535,
    longTyp = 4294967285UL,
    long_longTyp = 18446744073709551615ULL
};

enum class open_modes; //前置声明,使用默认成员类型int
enum class open_modes { input, output, append }; //定义
  • Front Statement Some notes:
    • Like other declarations and statements, enum declarations and definitions must match , which means that all of the enum size of the members must be consistent declarations and definitions
    • Moreover, we can not in the same context does not declare a scoped enum name, and then scoped enum declaration of the same name
    • Here are some presentation cases
enum class intValues;
enum intValues;       //错误,intValues已经被声明限定作用域的enum
enum intVales :long;  //错误,intValues已经被声明为int

Six, parameters match with enumerations

  • To initialize an enum objects, you must use another object of the enum type of an enumeration or its members
  • Therefore, even if an integer value is exactly equal to the value enumeration member, it can not be used as an argument to use enum function
enum Tokens { INLINE = 128, VIRTUAL = 129 };

void ff(Tokens)
{
    std::cout << "Tokens" << std::endl;
}
void ff(int)
{
    std::cout << "int" << std::endl;
}

int main()
{
    Tokens curTok = INLINE;
    ff(128);
    ff(INLINE);
    ff(curTok);

    return 0;
}

  • While we can not directly enum integer value passed to the parameter , but may be not limited to an enumerated type of object or enumeration member scope passed integer parameter . At this time, to enhance the value of enum integer int or larger, the result is determined by the actual lift of the potential of the type enumerated type:
enum Tokens { INLINE = 128, VIRTUAL = 129 };

void newf(unsigned char)
{
    std::cout << "unsigned char" << std::endl;
}
void newf(int)
{
    std::cout << "int" << std::endl;
}
int main()
{
    unsigned char uc = VIRTUAL;
    newf(VIRTUAL);
    newf(uc);

    return 0;
}

  • Enumerated type enumeration Tokends only two members, the larger value is 129. The enum type may be represented by unsigned char, many compilers use unsigned char as a potential type of Tokens. No matter what type of Tokens potential is its object and enumeration members are promoted to int. In particular, the enumeration member will never ascend into unsigned char, even though the enumeration values ​​can be stored, too unsigned char
Released 1504 original articles · won praise 1063 · Views 430,000 +

Guess you like

Origin blog.csdn.net/qq_41453285/article/details/104704584