zoukankan      html  css  js  c++  java
  • enum

    enum是一个好东西。

    In C and C++, enum types can be used to set up collections of named integer constants. (The keyword enum is short for ``enumerated''.)

    syntax:

     enum name {name-list} var-list;

    The enum keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration.

    如:

    enum MyEnumType { ALPHA, BETA, GAMMA };

    ALPHA has a value of 0, BETA has a value of 1, and GAMMA has a value of 2.

    If you want, you may provide explicit values for enum constants, as in

    enum FooSize { SMALL = 10, MEDIUM = 100, LARGE = 1000 };

    usage

    If you give an enum type a name, you can use that type for variables, function arguments and return values, and so on:

    enum MyEnumType x;  /* legal in both C and C++ */
    MyEnumType y;       // legal only in C++

    C++ enum type conversion rules

    The traditional C way of doing this was something like this:

    #define SPRING   0
    #define SUMMER   1
    #define FALL     2
    #define WINTER   3
    
    An alternate approach using enum would be
    enum { SPRING, SUMMER, FALL, WINTER };

    There is an implicit conversion from any enum type to int. Suppose this type exists:

    enum MyEnumType { ALPHA, BETA, GAMMA };
    
    Then the following lines are legal:
    int i = BETA;      // give i a value of 1
    int j = 3 + GAMMA; // give j a value of 5
    

    On the other hand, there is not an implicit conversion from int to an enum type:

    MyEnumType x = 2;    // should NOT be allowed by compiler
    MyEnumType y = 123;  // should NOT be allowed by compiler
    

    Note that it doesn't matter whether the int matches one of the constants of the enum type; the type conversion is always illegal.

    这里可以说明enum的一个优点。

    待补充..

  • 相关阅读:
    菜刀失效以后得到web的方法
    mimikatz使用
    hydra使用教程
    web安全学习-攻击会话管理
    web安全学习-验证机制存在的问题
    web安全学习-绕过客户端限制
    基于MaxCompute/Dataworks实现数据仓库管理与全链路数据体系
    tensorflow学习|基于TF-IDF的垃圾短信预测
    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...
    CentOS7下安装CDH,clouderamanager,hadoop
  • 原文地址:https://www.cnblogs.com/justin_s/p/1899132.html
Copyright © 2011-2022 走看看